Remember
lambda creates a function from a single expression. The expression’s value becomes the return value.
lambda creates a function from a single expression. The expression’s value becomes the return value.
Follow the example, change its sample, and compare what happens.
Explain the result and test your understanding.
Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.
Follow the code and watch its state change. Guided simulation.
double = lambda number: number * 2
print(double(3))Top-level scope
function <lambda>lambda creates a function from a single expression. The expression’s value becomes the return value.
(no output)
What do you expect this program to print?
EXAMPLE INSTRUCTIONS
double = lambda number: number * 2
print(double(3))
|
v
DISPLAYED RESULT
6
FINAL BINDINGS
double --> object 1 (function)
object 1: function <lambda>Choose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.
Choose a sample and follow its execution. Guided simulation.
double = lambda number: number * 2
print(double(3))Top-level scope
function <lambda>The example has finished. Compare the final output with the values and references above.
6
What do you expect this program to print?
double = lambda number: number * 2
print(double(3))A lambda function is a small, anonymous function defined using the lambda keyword instead of def. Unlike regular functions, lambda functions don’t have a name and are generally used for short, throwaway operations.
The basic syntax of a lambda function in Python is:
lambda arguments: expression
A lambda function can take any number of arguments, but it must contain only a single expression. It returns the result of that expression automatically — no need to use the return keyword.
add = lambda x, y: x + y
print(add(5, 3))
8
lambda x, y: defines an anonymous function that takes two parameters: x and y.x + y is evaluated and returned as the result of the function.add, so you can call it like a regular function using add(5, 3).8, which is printed to the output.Lambda functions are particularly useful when passed as arguments to higher-order functions. Consider the example below where we sort a list of tuples by the second element:
data = [(1, 4), (2, 1), (3, 9)]
sorted_data = sorted(data, key=lambda x: x[1])
print(sorted_data)
[(2, 1), (1, 4), (3, 9)]
Use lambda with filter() to select only even numbers from a list:
numbers = [1, 2, 3, 4, 5, 6]
evens = list(filter(lambda x: x % 2 == 0, numbers))
print(evens)
[2, 4, 6]
Apply a function to every element using map():
nums = [1, 2, 3, 4]
squares = list(map(lambda x: x**2, nums))
print(squares)
[1, 4, 9, 16]
map, filter, sorted).Lambda functions come with limitations. Avoid them when:
if/else, try/except, and print() inside lambda are not allowed unless used as expressions.max_func = lambda a, b: a if a > b else b
print(max_func(10, 20))
20
def add_def(x, y):
return x + y
add_lambda = lambda x, y: x + y
print(add_def(2, 3)) # Output: 5
print(add_lambda(2, 3)) # Output: 5
5
5
type() to confirm it's a function: print(type(lambda x: x))Connect what you noticed in the example with the idea below.
lambda creates a function from a single expression. The expression’s value becomes the return value.
A lambda body can contain a sequence of ordinary statements.
This is the misconception to avoid.Reaching the recap and completing the quizzes are separate milestones.