Bird
Raised Fist0
Pythonprogramming~10 mins

Lambda syntax and behavior in Python - Step-by-Step Execution

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Concept Flow - Lambda syntax and behavior
Define lambda function
Call lambda with argument
Execute expression inside lambda
Return result
Use result in program
A lambda is a small unnamed function defined and called with an argument, which returns the result of a simple expression.
Execution Sample
Python
add_one = lambda x: x + 1
result = add_one(5)
print(result)
Defines a lambda that adds 1 to input, calls it with 5, and prints 6.
Execution Table
StepActionExpression EvaluatedResultOutput
1Define lambda add_onelambda x: x + 1Function object assigned to add_one
2Call add_one with 5add_one(5)x + 1 with x=5
3Evaluate expression5 + 16
4Return result66
5Print resultprint(6)6
💡 Program ends after printing the result 6
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 4Final
add_oneundefinedlambda functionlambda functionlambda functionlambda function
resultundefinedundefined666
Key Moments - 3 Insights
Why does the lambda not have a name like a normal function?
The lambda is assigned to a variable (add_one) but itself is anonymous; this is shown in step 1 where the lambda function object is assigned to add_one.
What happens when we call add_one(5)?
Step 2 and 3 show the lambda is called with 5, then the expression x + 1 is evaluated with x=5, resulting in 6.
Can lambda functions have multiple statements?
No, lambda functions can only have one expression, as shown in step 3 where only 'x + 1' is evaluated.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of 'result' after step 3?
A5
B6
Cundefined
Dlambda function
💡 Hint
Check the 'Result' column at step 3 in the execution_table.
At which step is the lambda function assigned to the variable 'add_one'?
AStep 1
BStep 2
CStep 4
DStep 5
💡 Hint
Look at the 'Action' column for the assignment in the execution_table.
If we change the lambda to 'lambda x: x * 2', what would be the output at step 5 when called with 5?
A6
B5
C10
DError
💡 Hint
Multiply 5 by 2 as per the new lambda expression; see step 3 for expression evaluation.
Concept Snapshot
Lambda syntax:
lambda arguments: expression

- Creates a small anonymous function
- Can be assigned to variables
- Returns the value of the expression
- Used for simple, short functions
- Called like normal functions
Full Transcript
This visual trace shows how a lambda function is defined, called, and returns a result. First, the lambda expression 'lambda x: x + 1' is assigned to the variable 'add_one'. Then, calling add_one(5) evaluates the expression inside the lambda with x=5, resulting in 6. Finally, the result is printed. Lambdas are anonymous functions used for simple expressions and can be assigned to variables for reuse.

Practice

(1/5)
1. What does a lambda function in Python do?
easy
A. Creates a small anonymous function with a single expression
B. Defines a class with methods
C. Declares a variable
D. Imports a module

Solution

  1. Step 1: Understand lambda purpose

    A lambda function is a quick way to create a function without a name that returns the result of one expression.
  2. Step 2: Compare options

    Only Creates a small anonymous function with a single expression describes this behavior correctly. Other options describe unrelated Python features.
  3. Final Answer:

    Creates a small anonymous function with a single expression -> Option A
  4. Quick Check:

    Lambda = anonymous single-expression function [OK]
Hint: Lambdas are short unnamed functions with one expression [OK]
Common Mistakes:
  • Thinking lambda can have multiple statements
  • Confusing lambda with class or variable definitions
  • Assuming lambda needs a name
2. Which of the following is the correct syntax for a lambda function that adds two numbers x and y?
easy
A. lambda (x, y) { return x + y }
B. lambda x, y: x + y
C. def lambda(x, y): return x + y
D. lambda x, y => x + y

Solution

  1. Step 1: Recall lambda syntax

    In Python, lambda syntax is: lambda parameters: expression
  2. Step 2: Check each option

    lambda x, y: x + y matches correct syntax. Options B and D use other language styles. def lambda(x, y): return x + y wrongly uses def with lambda.
  3. Final Answer:

    lambda x, y: x + y -> Option B
  4. Quick Check:

    Correct lambda syntax = lambda params: expression [OK]
Hint: Remember: lambda params colon expression [OK]
Common Mistakes:
  • Using parentheses around parameters in lambda
  • Trying to use curly braces or return keyword
  • Mixing lambda with def syntax
3. What is the output of this code?
func = lambda x: x * 2
print(func(5))
medium
A. 10
B. 25
C. SyntaxError
D. None

Solution

  1. Step 1: Understand the lambda function

    The lambda takes input x and returns x multiplied by 2.
  2. Step 2: Calculate func(5)

    func(5) = 5 * 2 = 10
  3. Final Answer:

    10 -> Option A
  4. Quick Check:

    5 * 2 = 10 [OK]
Hint: Multiply input by 2 as lambda defines [OK]
Common Mistakes:
  • Confusing multiplication with addition
  • Expecting syntax error due to lambda
  • Thinking lambda returns None by default
4. Find the error in this lambda function:
double = lambda x: return x * 2
print(double(4))
medium
A. No error, code runs fine
B. Missing parentheses around x
C. Lambda cannot multiply numbers
D. Using 'return' inside lambda causes SyntaxError

Solution

  1. Step 1: Check lambda syntax rules

    Lambdas cannot contain statements like 'return'; they only have an expression.
  2. Step 2: Identify error in code

    The code uses 'return' inside lambda, which is invalid and causes SyntaxError.
  3. Final Answer:

    Using 'return' inside lambda causes SyntaxError -> Option D
  4. Quick Check:

    Lambda disallows 'return' keyword [OK]
Hint: Lambdas never use 'return' keyword [OK]
Common Mistakes:
  • Adding 'return' inside lambda
  • Thinking parentheses are mandatory around parameters
  • Believing lambda can't do math operations
5. You want to create a list of squares for numbers 1 to 5 using a lambda inside map. Which code correctly does this?
hard
A. list(map(lambda x: x*2, range(1, 5)))
B. map(lambda x: x**2, [1, 2, 3, 4, 5])
C. list(map(lambda x: x**2, range(1, 6)))
D. list(map(lambda x: x**2, range(5)))

Solution

  1. Step 1: Understand the goal

    Create squares of numbers 1 to 5 inclusive, so numbers are 1,2,3,4,5.
  2. Step 2: Check each option

    list(map(lambda x: x**2, range(1, 6))) uses range(1,6) which includes 1 to 5 and squares each number correctly.
    list(map(lambda x: x*2, range(1, 5))) doubles numbers and uses range(1,5) which excludes 5.
    map(lambda x: x**2, [1, 2, 3, 4, 5]) returns a map object, not a list.
    list(map(lambda x: x**2, range(5))) uses range(5) which is 0 to 4, not 1 to 5.
  3. Final Answer:

    list(map(lambda x: x**2, range(1, 6))) -> Option C
  4. Quick Check:

    Squares 1-5 with map and lambda [OK]
Hint: Use range(1,6) and lambda x: x**2 inside list(map()) [OK]
Common Mistakes:
  • Using range(5) which starts at 0
  • Forgetting to convert map to list
  • Using wrong lambda expression like x*2