Update README.md

Signed-off-by: David Rotermund <54365609+davrot@users.noreply.github.com>
This commit is contained in:
David Rotermund 2023-12-13 15:44:48 +01:00 committed by GitHub
parent 657e9b0d88
commit d5fe6ff6a2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -8,10 +8,12 @@
## 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)
**Logic blocks need to be indented. Preferable with 4 spaces!**
## [Most simple function](https://docs.python.org/3/tutorial/controlflow.html#defining-functions)
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(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.