Concept
Lambda Functions: Throwaway Anonymous Functions
A `lambda` is an unnamed inline function defined in one line: `lambda params: expression`. It automatically returns the expression's result.
python
double = lambda x: x * 2
print(double(5)) # 10
# 103. Lambda Functions: Throwaway Anonymous Functions A `lambda` is an unnamed inline function defined in one line: `lambda params: expression`. It automatically returns the expression's result: ```python double = lambda x: x * 2 print(double(5)) # 10 ``` Lambdas are restricted to a single expression. You cannot put statements, loops, or assignments inside them. Use them as quick throwaway callbacks for `sorted(items, key=lambda x: x['age'])`.
Checkpoint
Why don't you write the `return` keyword inside a `lambda`?
Practice
Create a lambda function that adds two numbers `a` and `b`. Assign it to `add` and print `add(10, 20)`.
01
Tap code blocks below to assemble
Available Blocks