From 657e9b0d88f7516145aa38b63099cd4d1e114043 Mon Sep 17 00:00:00 2001 From: David Rotermund <54365609+davrot@users.noreply.github.com> Date: Wed, 13 Dec 2023 15:36:09 +0100 Subject: [PATCH] Update README.md Signed-off-by: David Rotermund <54365609+davrot@users.noreply.github.com> --- python_basics/functions/README.md | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/python_basics/functions/README.md b/python_basics/functions/README.md index ce54375..5b51061 100644 --- a/python_basics/functions/README.md +++ b/python_basics/functions/README.md @@ -304,7 +304,7 @@ a: 4 b: 5 ``` -[Unpacking Argument Lists](https://docs.python.org/3/tutorial/controlflow.html#unpacking-argument-lists) +## [Unpacking Argument Lists](https://docs.python.org/3/tutorial/controlflow.html#unpacking-argument-lists) ```python def f(a: int, b: int, c: int, d: int) -> int: @@ -324,3 +324,29 @@ print(f(2, *my_tuple_c)) # -> 120 ``` ## Documentation strings​ + +## [Lambda expressions / anonymous functions​](https://docs.python.org/3/reference/expressions.html#lambda) + +```python +lambda_expr ::= "lambda" [parameter_list] ":" expression +``` +> Lambda expressions (sometimes called lambda forms) are used to create anonymous functions. The expression lambda parameters: expression yields a function object. The unnamed object behaves like a function object defined with: + +```python +def (parameters): + return expression +``` + +Example 1: + +```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')] + +print((lambda x: x[1])(pairs)) # -> (2, 'two') +print(f(pairs)) # -> (2, 'two') +```