Lesson 4 ❮ Lesson List ❮ Top Page
❯ 4.2 Lambda Functions
⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺
EXPECTED COMPLETION TIME
❲▹❳ Video 9m 44s
☷ Interactive readings 5m
✑ Practice 4.2 (G Colab) 20m
To make a function return a value, use the return statement. Having a return value means you can save the result of the function into a variable.
A lambda function is a small anonymous function.
It can only have one expression and will also return that one expression. In this example, we will use a generator expression.
While you can add a generator expression, lambda functions are commonly used for simple calculation.
It can accept multiple arguments as well.
Lambda functions are best for use for smaller functions (only one line of code). If a function is more than one line, it is recommended to use def instead.
Instead of writing a long list comprehension, you can split it into a lambda function and a shorter list comprehension.