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
Recall & Review
beginner
What is a lambda function in Python?
A lambda function is a small anonymous function defined with the lambda keyword. It can take any number of arguments but has only one expression, which is returned automatically.
Click to reveal answer
beginner
How do you write a lambda function that adds 5 to its input?
You write it as lambda x: x + 5. This means the function takes one argument x and returns x + 5.
Click to reveal answer
beginner
Can a lambda function contain multiple expressions or statements?
No, a lambda function can only contain a single expression. It cannot have multiple statements or commands like loops or assignments.
Click to reveal answer
intermediate
How does a lambda function differ from a regular function defined with def?
A lambda function is anonymous (has no name) and is limited to one expression. A regular function can have multiple statements, a name, and more complex behavior.
Click to reveal answer
beginner
What is the output of this code?
add = lambda x, y: x + y
print(add(3, 4))
The output is 7. The lambda function adds the two inputs 3 and 4 and returns the sum.
Click to reveal answer
What keyword is used to create a lambda function in Python?
Alambda
Bdef
Cfunc
Dfunction
✗ Incorrect
The lambda keyword is used to create anonymous functions in Python.
Which of the following is a valid lambda function syntax?
Adef lambda(x, y): return x + y
Blambda (x, y) { return x + y }
Clambda x, y: x + y
Dlambda x, y { x + y }
✗ Incorrect
Option C is the correct syntax for a lambda function in Python.
What will this lambda function return?
lambda x: x * 2
ADouble the input value
BHalf the input value
CAlways returns 2
DReturns the input unchanged
✗ Incorrect
The lambda function multiplies the input by 2, so it returns double the input.
Can a lambda function have multiple statements inside it?
AOnly if using semicolons
BYes, multiple statements separated by commas
CYes, if enclosed in parentheses
DNo, only one expression is allowed
✗ Incorrect
Lambda functions are limited to a single expression and cannot contain multiple statements.
What is the main use of lambda functions?
ATo define classes
BTo create small anonymous functions quickly
CTo replace all regular functions
DTo write multi-line functions
✗ Incorrect
Lambda functions are mainly used for quick, small anonymous functions, often as arguments to other functions.
Explain what a lambda function is and how it differs from a regular function defined with def.
Think about the size and naming of the function.
You got /5 concepts.
Write a lambda function that takes two numbers and returns their product. Then explain how it works.
Use the format: lambda arguments: expression
You got /4 concepts.
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
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.
Step 2: Compare options
Only Creates a small anonymous function with a single expression describes this behavior correctly. Other options describe unrelated Python features.
Final Answer:
Creates a small anonymous function with a single expression -> Option A
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
Step 1: Recall lambda syntax
In Python, lambda syntax is: lambda parameters: expression
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.
The lambda takes input x and returns x multiplied by 2.
Step 2: Calculate func(5)
func(5) = 5 * 2 = 10
Final Answer:
10 -> Option A
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
Step 1: Check lambda syntax rules
Lambdas cannot contain statements like 'return'; they only have an expression.
Step 2: Identify error in code
The code uses 'return' inside lambda, which is invalid and causes SyntaxError.
Final Answer:
Using 'return' inside lambda causes SyntaxError -> Option D
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
Step 1: Understand the goal
Create squares of numbers 1 to 5 inclusive, so numbers are 1,2,3,4,5.
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.
Final Answer:
list(map(lambda x: x**2, range(1, 6))) -> Option C
Quick Check:
Squares 1-5 with map and lambda [OK]
Hint: Use range(1,6) and lambda x: x**2 inside list(map()) [OK]