From d5fe6ff6a22750d929c995ba775b451946688e18 Mon Sep 17 00:00:00 2001 From: David Rotermund <54365609+davrot@users.noreply.github.com> Date: Wed, 13 Dec 2023 15:44:48 +0100 Subject: [PATCH] Update README.md Signed-off-by: David Rotermund <54365609+davrot@users.noreply.github.com> --- python_basics/functions/README.md | 40 ++++++++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/python_basics/functions/README.md b/python_basics/functions/README.md index 5b51061..795562b 100644 --- a/python_basics/functions/README.md +++ b/python_basics/functions/README.md @@ -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. + +