Update README.md
Signed-off-by: David Rotermund <54365609+davrot@users.noreply.github.com>
This commit is contained in:
parent
657e9b0d88
commit
d5fe6ff6a2
1 changed files with 39 additions and 1 deletions
|
@ -8,10 +8,12 @@
|
||||||
|
|
||||||
## The goal
|
## The goal
|
||||||
|
|
||||||
|
A function allows to seperate a part of the code in a logical module that can be reused.
|
||||||
|
|
||||||
Questions to [David Rotermund](mailto:davrot@uni-bremen.de)
|
Questions to [David Rotermund](mailto:davrot@uni-bremen.de)
|
||||||
|
|
||||||
|
**Logic blocks need to be indented. Preferable with 4 spaces!**
|
||||||
|
|
||||||
## [Most simple function](https://docs.python.org/3/tutorial/controlflow.html#defining-functions)
|
## [Most simple function](https://docs.python.org/3/tutorial/controlflow.html#defining-functions)
|
||||||
|
|
||||||
These three functions are equivalent:
|
These three functions are equivalent:
|
||||||
|
@ -350,3 +352,39 @@ print(pairs) # -> [(1, 'one'), (2, 'two'), (3, 'three'), (4, 'four')]
|
||||||
print((lambda x: x[1])(pairs)) # -> (2, 'two')
|
print((lambda x: x[1])(pairs)) # -> (2, 'two')
|
||||||
print(f(pairs)) # -> (2, 'two')
|
print(f(pairs)) # -> (2, 'two')
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Example 2:
|
||||||
|
|
||||||
|
With a normal function:
|
||||||
|
|
||||||
|
```python
|
||||||
|
pairs = [(1, "one"), (2, "two"), (3, "three"), (4, "four")]
|
||||||
|
|
||||||
|
def f(x):
|
||||||
|
return x[1]
|
||||||
|
|
||||||
|
print(pairs) # -> [(1, 'one'), (2, 'two'), (3, 'three'), (4, 'four')]
|
||||||
|
|
||||||
|
pairs.sort(key=f)
|
||||||
|
print(pairs) # -> [(4, 'four'), (1, 'one'), (3, 'three'), (2, 'two')]
|
||||||
|
```
|
||||||
|
|
||||||
|
```python
|
||||||
|
pairs = [(1, "one"), (2, "two"), (3, "three"), (4, "four")]
|
||||||
|
|
||||||
|
|
||||||
|
print(pairs) # -> [(1, 'one'), (2, 'two'), (3, 'three'), (4, 'four')]
|
||||||
|
|
||||||
|
pairs.sort(key=(lambda x: x[1]))
|
||||||
|
print(pairs) # -> [(4, 'four'), (1, 'one'), (3, 'three'), (2, 'two')]
|
||||||
|
```
|
||||||
|
|
||||||
|
Small warning:
|
||||||
|
|
||||||
|
```python
|
||||||
|
print(pairs.sort()) # -> None
|
||||||
|
```
|
||||||
|
|
||||||
|
because **.sort()** is an inplace function.
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue