Skip to content

Commit

Permalink
Merge branch 'master' into syntax-and-numerical-operators
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefan-Stojanovic authored Jan 10, 2022
2 parents 8faf01a + 98a5b69 commit 7c7c232
Show file tree
Hide file tree
Showing 37 changed files with 150 additions and 187 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,17 @@ Types of change:
- [Html - Link Relative Paths - Change part of PQ as it wasn't worder properly](https://github.com/enkidevs/curriculum/pull/2985)
- [Python - Format Text Paragraphs With Textwrap - Make the fill method more clear](https://github.com/enkidevs/curriculum/pull/2981)
- [Python - Syntax And Numerical Operators - Move single-line commands to a single line, update indentation in codeblocks from 4 to 2 spaces](https://github.com/enkidevs/curriculum/pull/3014)
- [Python - Testing - Move single-line commands to a single line, update indentation in codeblocks from 4 to 2 spaces](https://github.com/enkidevs/curriculum/pull/3015)
- [Python - Unordered Data Types - Move single-line commands to a single line, update indentation in codeblocks from 4 to 2 spaces](https://github.com/enkidevs/curriculum/pull/3016)
- [Python - Utilities I - Move single-line commands to a single line, update indentation in codeblocks from 4 to 2 spaces](https://github.com/enkidevs/curriculum/pull/3017)
- [Python - Utilities II - Move single-line commands to a single line, update indentation in codeblocks from 4 to 2 spaces](https://github.com/enkidevs/curriculum/pull/3018)
- [Python - Working With Strings - Move single-line commands to a single line, update indentation in codeblocks from 4 to 2 spaces](https://github.com/enkidevs/curriculum/pull/3019)
- [Python - Arrays I - Move single-line commands to a single line, update indentation in codeblocks from 4 to 2 spaces](https://github.com/enkidevs/curriculum/pull/3020)
- [Python - Arrays II - Move single-line commands to a single line, update indentation in codeblocks from 4 to 2 spaces](https://github.com/enkidevs/curriculum/pull/3021)
- [Python - Comprehension - Move single-line commands to a single line, update indentation in codeblocks from 4 to 2 spaces](https://github.com/enkidevs/curriculum/pull/3022)
- [Python - Functional Programming - Move single-line commands to a single line, update indentation in codeblocks from 4 to 2 spaces](https://github.com/enkidevs/curriculum/pull/3024)
- [Python - Generators - Move single-line commands to a single line, update indentation in codeblocks from 4 to 2 spaces](https://github.com/enkidevs/curriculum/pull/3026)
- [Python - Python Immutability - Move single-line commands to a single line, update indentation in codeblocks from 4 to 2 spaces](https://github.com/enkidevs/curriculum/pull/3028)
- [Python - Iterators - Move single-line commands to a single line, update indentation in codeblocks from 4 to 2 spaces](https://github.com/enkidevs/curriculum/pull/3027)

## January 4th 2022
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ The `reversed` built-in allows us to create an iterator for an iterable sequence
reversed(seq)
```

Where `seq` is an iterable sequence such as a tuple, list, string or range. This returns an iterator which accesses the elements in the sequence in the reverse order. For example, we may use `reversed` to reverse the order of characters in a string.
Where `seq` is an iterable sequence such as a tuple, list, string, or range. This returns an iterator, which accesses the elements in the sequence in the reverse order. For example, we may use `reversed` to reverse the order of characters in a string.

```python
ourString = 'enki'
print(list(reversed(ourString)))
# Result: ['i', 'k', 'n', 'e']
```

Notice how we could create custom classes that implement the `__reversed__()` method and then use `reversed` on them to quickly and efficiently reverse their ordering. In this way we can begin to see how often the `reversed` function might be useful in day-to-day programming tasks.
Notice how we could create custom classes that implement the `__reversed__()` method and then use `reversed` on them to quickly and efficiently reverse their ordering. In this way, we can begin to see how often the `reversed` function might be useful in day-to-day programming tasks.


---
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,7 @@ print(ourString[sObject])
Use `slice` to remove every second number in the list of numbers.

```python
nList = ['1', '2', '3', '4', '5',
'6', '7', '8']
nList = ['1', '2', '3', '4', '5', '6', '7', '8']

sObject = ???(???, ???, ???)
print(nList[sObject])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,18 +105,9 @@ We have three lists, `fnames`, `lnames`, `locations`, which are ordered so that
Fill in the gaps in the code below to achieve this.

```python
locations = ['IT',
'FR',
'FR',
'RU']
fnames = ['italo',
'jean',
'emily',
'katya']
lnames = ['calvino',
'micheal',
'rambert',
'sokolov']
locations = ['IT', 'FR', 'FR', 'RU']
fnames = ['italo', 'jean', 'emily', 'katya']
lnames = ['calvino', 'micheal', 'rambert', 'sokolov']

result = zip(???, ???)
result2 = zip(???, ???)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ revisionQuestion:

## Content

The `map` function is built in to the Python language. Together with the other built-in functions `filter` and `reduce`, `map` allows us to take a functional approach to programming in Python.
The `map` function is built into the Python language. Together with the other built-in functions `filter` and `reduce`, `map` allows us to take a functional approach to programming in Python.

`map` applies a given function—`function_here` in the case below—iteratively to all items in a given `input_list`[1]. The basic syntax looks like this:

Expand Down Expand Up @@ -68,8 +68,7 @@ Finally, it's good to know that we can pass more than one iterable `input_list`
Let's say we have a list, called `promises`. We want to `make_good` on all our promises, where `make_good` is a previously-defined function that takes a string. Fill in the blanks in the code below to apply `make_good` to all elements in `promises`.

```python
promises = ['learn css', 'learn js',
'buy milk', 'be excellent to each other']
promises = ['learn css', 'learn js', 'buy milk', 'be excellent to each other']
promises = ???(???, ???)
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,7 @@ print(sorted([4, 0, 2, 3, 1, 5]))
What is the result of the execution of the following code snippet?

```python
print(sorted([0, 2, 3, 1,
'a', 'b', 'A', 'B']))
print(sorted([0, 2, 3, 1, 'a', 'b', 'A', 'B']))
```

???
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ cube_dict = {x: x ** 3 for x in num_list}

```

Now if we print cube_dict, we get:
Now, if we print `cube_dict`, we get:

```python
for k, v in cube_dict.items():
print(k, v)
print(k, v)
# output
# 1 1
# 2 8
Expand All @@ -58,17 +58,18 @@ print(lcase_freqs)
# partial output ...

{'u': 0, 'q': 0, 'w': 0, 'o': 0, \
'b': 0, 'c': 0, 't': 0, 'h': 0, \
'b': 0, 'c': 0, 't': 0, 'h': 0, \
...
'g': 0, 'a': 0, 'n': 0}
'g': 0, 'a': 0, 'n': 0}

# Check it is correct:
lfk = list(lcase_freqs.keys())
lfk.sort()
print(lfk)
['a', 'b', 'c', 'd', 'e', 'f', \
'g', 'h', 'i', 'j', 'k', 'l', \
'm', 'n', 'o', 'p','q', 'r', \
's', 't', 'u', 'v', 'w', 'x', \
'y', 'z']
'g', 'h', 'i', 'j', 'k', 'l', \
'm', 'n', 'o', 'p','q', 'r', \
's', 't', 'u', 'v', 'w', 'x', \
'y', 'z']

```
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,7 @@ Use list comprehension to add one and divide by two [(x + 1) / 2] for all elemen

```python
l = [1,2,3,4,5]
x = [((x+1)/2) ??? x % 2 \
??? x ??? x in ???]
x = [((x+1)/2) ??? x % 2 ??? x ??? x in ???]
```

- if
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,27 +27,23 @@ Since a list comprehension can take any **expression** as its initial expression
These are often useful, but are often used to work with matrices.

```python
matrix = [[1, 2, 3], [4, 5, 6], \
[7, 8, 9]]

matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
```

Say we want to create another matrix with values equal to the squares of each element in the original matrix:

```python
matrix2 = [[x**2 for x in row] for \
row in matrix]
#matrix2 = [[1, 4, 9], [16, 25, 36],\
# [49, 64, 81]]
matrix2 = [[x**2 for x in row] for row in matrix]
#matrix2 = [[1, 4, 9], [16, 25, 36], [49, 64, 81]]
```

A more advanced list comprehension with two for clauses and two if clauses:

```python
lc = [ (x, y) for x in \
range(10) if x % 2 == 0 \
for y in range(20) if \
y % 3 == 0 ]
range(10) if x % 2 == 0 \
for y in range(20) if \
y % 3 == 0 ]
# lc
# [(0, 0), (0, 3), (0, 6), \
# (0, 9), (0, 12), (0, 15), (0, 18),\
Expand All @@ -70,7 +66,6 @@ Use nested list comprehension to generate a list of tuples, where the first elem
Ex: (1,1),(1,2),(1,3),...(9,7),(9,8),(9,9).

```python

l = [??? for x in range(10)\
if ??? for y in ???]
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,19 @@ Imagine we have the following list:

```python
my_list = [1, 2, 3, 4, 5, 6, 7, 8]

```

And we need a set containing only even numbers in the list. This can be easily achieved with **set comprehension**:

```python
even_set = {x for x in my_list if x%2 == 0}

```

We can now check the result:

```python
print(even_set)
# {8, 2, 4, 6}

```

Note that the above operation would work even if my_list contained some duplicate values, e.g:
Expand All @@ -67,10 +64,8 @@ since sets by definition do not allow duplicates.
Fill in the following code snippet. It creates a new set that contains elements of list `l` that are even and adds one and divides by two the odd numbers:

```python

l = [10, 11, 13, 14, 18, 19]
new_set = {x ??? x % 2 == 0 else/
??? for x ??? l}
new_set = {x ??? x % 2 == 0 else ??? for x ??? l}
```

- if
Expand All @@ -88,7 +83,6 @@ new_set = {x ??? x % 2 == 0 else/
What will the `odd_set` look like after we run the following code snippet?

```python

l = [1,3,3,2,4,5,5,8,9]
odd_set = {x for x in l if x % 2}
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ If you need to lowercase all the input strings:
```python
lower_list = []
for word in input_list:
lower_list.append(word.lower())
lower_list.append(word.lower())
```

Instead, you can use `map()` to push the loop into compiled C code:
Expand All @@ -47,8 +47,7 @@ lower_list = map(str.lower, input_list)
Also, in Python 2.0 or above, there are list comprehensions. List comprehension are the "pythonic" way to approach this situation. `map()` is more often used in JavaScript. We recommend usage of list comprehension:

```python
lower_list = [word.lower() \
for word in input_list]
lower_list = [word.lower() for word in input_list]
```

They are both more efficient than simple `for` loop statement.
Expand All @@ -63,8 +62,7 @@ Use list comprehension to modify a list of characters such that all its elements
```python
strings = ['a', 'e', 'i', 'o', 'u']

lower_list = [word.??? \
for word in ???]
lower_list = [word.??? for word in ???]
```

- upper()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ This is a way to define functions in a one-line fashion. Functions defined with

```py
foo = [1, 2, 3, 4, 5, 6]
print(list(filter(
lambda x: x % 2 == 0,foo))
)
print(list(filter( \
lambda x: x % 2 == 0,foo
)))

# Output: [2, 4, 6]
```
Expand Down Expand Up @@ -108,10 +108,9 @@ Can you predict what the output will be?
```py
foo = list(range(1,10))

result = list(
filter(
lambda x: x / 2 == 1 ,foo
))
result = list(filter( \
lambda x: x / 2 == 1 ,foo
))

print(result)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Last insight, we've seen how **recursion** and **generators** can work together.

Consider the following example:

```plain-text
```python
def fibonacci():
#Generating fibonacci sequence
a, b = 0, 1
Expand All @@ -51,7 +51,7 @@ This is why we define the second **generator** called `firstn` which accepts two

Finally, we print a list containing the first 10 *elements* of the *Fibonacci sequence*:

```plain-text
```python
# Output:
# [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
```
Expand Down Expand Up @@ -99,8 +99,7 @@ def n_power(g,n):
for i in range(n):
yield next(g)

print(list(n_power(
power_of_two(), 4)))
print(list(n_power(power_of_two(), 4)))
```

???
Expand Down
16 changes: 8 additions & 8 deletions python/functional-programming/generators/recursive-generator.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ Consider the following example:

```python
def infinity(start):
yield start
for x in infinity(start + 1)
yield x
yield start
for x in infinity(start + 1)
yield x
```

We defined a **generator** that counts up to infinity. During the first evaluation, the starting value will be **returned**. Then we loop on the new **generators** created in the `for`'s body.
Expand All @@ -43,8 +43,8 @@ Let's check out the example above implemented using `yield from`:

```python
def infinity(start):
yield start
yield from infinity(start + 1)
yield start
yield from infinity(start + 1)

gen = infinity(20)
print(next(gen)) # 20
Expand All @@ -64,9 +64,9 @@ Can you spot which of the following generators are recursive?

```python
def list_gen(l):
if l:
yield l[0]
yield from list_gen(l[1:])
if l:
yield l[0]
yield from list_gen(l[1:])

def cubic_generator(n):
for i in range(n):
Expand Down
14 changes: 7 additions & 7 deletions python/functional-programming/generators/yield-and-next.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ Consider the following generator:

```py
def range_gen(n):
i = 0
while i < n:
yield i
i += 1
i = 0
while i < n:
yield i
i += 1
```

This **function** generates all natural numbers up to `n`. Let's use the `next()` method now:
Expand Down Expand Up @@ -73,9 +73,9 @@ What is the output of the following snippet?

```py
def countdown(num):
while num > 0:
yield num
num -= 1
while num > 0:
yield num
num -= 1

>>> gen = countdown(5)
>>> print(next(gen))
Expand Down
Loading

0 comments on commit 7c7c232

Please sign in to comment.