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.
Click to reveal answer
beginner
Why do we use lambda functions instead of regular functions?
Lambda functions are used for short, simple functions that are needed temporarily, often as arguments to other functions, without the need to formally define a function using def.
Click to reveal answer
intermediate
Give an example of a situation where a lambda function is useful.
When sorting a list of tuples by the second item, you can use a lambda function as the key: sorted(list_of_tuples, key=lambda x: x[1]). This avoids writing a separate function.
Click to reveal answer
beginner
Can lambda functions have multiple expressions?
No, lambda functions can only have one expression. They return the result of that expression automatically.
Click to reveal answer
beginner
How do lambda functions help in making code more concise?
Lambda functions let you write small functions inline without naming them, which reduces the amount of code and makes it easier to read when the function is simple.
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 true about lambda functions?
AThey can be used as arguments to other functions.
BThey must have a name.
CThey cannot take any arguments.
DThey can have multiple expressions.
✗ Incorrect
Lambda functions are often used as arguments to other functions like map, filter, or sorted.
Why might you choose a lambda function over a regular function?
ATo improve performance significantly.
BTo create a function with multiple statements.
CTo define a function that can be reused many times.
DTo write a quick, simple function inline without naming it.
✗ Incorrect
Lambda functions are best for small, simple functions used temporarily, written inline.
Which of these is a valid lambda function syntax?
Alambda x, y { return x + y }
Bdef lambda(x, y): return x + y
Clambda x, y: x + y
Dfunction lambda(x, y) { return x + y }
✗ Incorrect
The correct syntax uses lambda followed by arguments, a colon, and a single expression.
What will this code output? print((lambda x: x * 2)(5))
A25
B10
C5
DError
✗ Incorrect
The lambda function doubles the input 5, so the output is 10.
Explain in your own words why lambda functions are useful in Python.
Think about when you want a quick function without naming it.
You got /4 concepts.
Describe a real-life example where using a lambda function can make your code simpler.
Consider how you might sort a list of items by a specific part.
You got /4 concepts.
Practice
(1/5)
1. Why are lambda functions commonly used in Python?
easy
A. To improve the speed of the program by compiling code
B. To define large and complex functions with many lines
C. To replace all regular functions in a program
D. To create small, unnamed functions quickly for simple tasks
Solution
Step 1: Understand the purpose of lambda functions
Lambda functions are designed to create small, unnamed functions quickly, usually for simple tasks.
Step 2: Compare with other options
Options A, B, and C describe uses that do not match lambda functions' purpose.
Final Answer:
To create small, unnamed functions quickly for simple tasks -> Option D
Quick Check:
Lambda functions = quick unnamed functions [OK]
Hint: Lambda = quick small function without a name [OK]
Common Mistakes:
Thinking lambda can replace all functions
Believing lambda is for complex logic
Confusing lambda with performance optimization
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: x + y
B. def lambda(x, y): return x + y
C. lambda (x, y) { return x + y }
D. function lambda(x, y) => x + y
Solution
Step 1: Recall lambda syntax in Python
Lambda functions use the syntax: lambda parameters: expression.
Step 2: Check each option
lambda x, y: x + y matches the correct syntax. Options B, C, and D use incorrect syntax styles.
Hint: map + lambda applies function to each list item [OK]
Common Mistakes:
Thinking map needs a named function
Confusing square with doubling
Expecting original list unchanged
4. Find the error in this code using a lambda function:
add = lambda x, y:
return x + y
print(add(3, 4))
medium
A. Indentation error in lambda body
B. Lambda functions cannot have more than one parameter
C. Lambda functions cannot use return statement
D. Missing colon after lambda parameters
Solution
Step 1: Check lambda function syntax
Lambda functions are single expressions and cannot use statements like return.
Step 2: Identify the error in the code
The code incorrectly uses return inside a lambda, which is not allowed.
Final Answer:
Lambda functions cannot use return statement -> Option C
Quick Check:
Lambda = single expression, no return [OK]
Hint: Lambda is one expression, no return keyword [OK]
Common Mistakes:
Adding return inside lambda
Expecting multi-line lambda bodies
Confusing lambda with def function syntax
5. You want to sort a list of tuples data = [("apple", 2), ("banana", 1), ("cherry", 3)] by the second item in each tuple using a lambda function. Which code correctly does this?
hard
A. data.sort(lambda x: x[1])
B. data.sort(key=lambda x: x[1])
C. data.sort(key=lambda x: x[0])
D. data.sort(key=lambda y: y[2])
Solution
Step 1: Understand sorting with key parameter
The key argument takes a function to extract the sorting value from each item.
Step 2: Check lambda usage for sorting by second tuple item
Lambda should return the second item: x[1]. data.sort(key=lambda x: x[1]) uses correct syntax.
Final Answer:
data.sort(key=lambda x: x[1]) -> Option B
Quick Check:
Sort by second item = key=lambda x: x[1] [OK]
Hint: Use key=lambda x: x[index] to sort by tuple item [OK]