Bird
Raised Fist0
Pythonprogramming~20 mins

Lambda vs regular functions in Python - Practice Questions

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
Challenge - 5 Problems
๐ŸŽ–๏ธ
Lambda Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
โ“ Predict Output
intermediate
2:00remaining
Output of lambda with default argument
What is the output of this code?
Python
funcs = [(lambda x=i: x*2) for i in range(3)]
results = [f() for f in funcs]
print(results)
A[0, 2, 4]
B[4, 4, 4]
C[0, 0, 0]
DTypeError
Attempts:
2 left
๐Ÿ’ก Hint
Think about how default arguments capture values at definition time.
โ“ Predict Output
intermediate
2:00remaining
Difference in output between lambda and regular function
What is the output of this code?
Python
def make_funcs():
    funcs = []
    for i in range(3):
        def f():
            return i * 2
        funcs.append(f)
    return funcs

funcs = make_funcs()
results = [f() for f in funcs]
print(results)
A[0, 2, 4]
B[4, 4, 4]
C[0, 0, 0]
DNameError
Attempts:
2 left
๐Ÿ’ก Hint
Consider when the variable i is looked up inside the function.
๐Ÿ”ง Debug
advanced
2:00remaining
Identify the error in lambda with multiple statements
Which option shows the correct way to write a lambda that returns the sum of two numbers and prints a message?
Alambda x, y: (print('Adding'), x + y)[1]
Blambda x, y: print('Adding') + (x + y)
Clambda x, y: {print('Adding'); return x + y}
Dlambda x, y: x + y; print('Adding')
Attempts:
2 left
๐Ÿ’ก Hint
Remember lambdas can only have expressions, not statements.
โ“ Predict Output
advanced
2:00remaining
Output of lambda capturing loop variable without default
What is the output of this code?
Python
funcs = []
for i in range(3):
    funcs.append(lambda: i)
results = [f() for f in funcs]
print(results)
A[0, 1, 2]
B[0, 0, 0]
C[2, 2, 2]
DSyntaxError
Attempts:
2 left
๐Ÿ’ก Hint
Think about when the variable i is evaluated inside the lambda.
๐Ÿง  Conceptual
expert
2:00remaining
Why use lambda instead of regular function?
Which of these is the best reason to use a lambda function instead of a regular function?
ATo create functions that can be called without parentheses
BTo improve performance by compiling faster than regular functions
CTo allow multiple statements inside the function body
DTo define a small anonymous function inline, especially as an argument to another function
Attempts:
2 left
๐Ÿ’ก Hint
Think about where lambdas are often used in Python code.

Practice

(1/5)
1. Which of the following best describes a lambda function in Python?
easy
A. A function defined using def that can have multiple statements.
B. A short, anonymous function defined with the lambda keyword.
C. A function that must always return None.
D. A function that can only be used inside classes.

Solution

  1. Step 1: Understand lambda function definition

    A lambda function is defined using the lambda keyword and is anonymous (no name).
  2. Step 2: Compare with regular functions

    Regular functions use def and can have multiple statements, unlike lambdas which are single expressions.
  3. Final Answer:

    A short, anonymous function defined with the lambda keyword. -> Option B
  4. Quick Check:

    Lambda = short anonymous function [OK]
Hint: Lambdas are short and nameless functions [OK]
Common Mistakes:
  • Thinking lambdas can have multiple statements
  • Confusing lambda with regular def functions
  • Believing lambdas must return None
2. Which of the following is the correct syntax for a lambda function that adds 5 to its input?
easy
A. def add_five(x): return x + 5
B. lambda x x + 5
C. lambda x: x + 5
D. lambda (x): return x + 5

Solution

  1. Step 1: Recall lambda syntax

    A lambda function is written as lambda parameters: expression without def or return.
  2. Step 2: Check each option

    lambda x: x + 5 matches the correct syntax: lambda x: x + 5. Others have syntax errors or use def.
  3. Final Answer:

    lambda x: x + 5 -> Option C
  4. Quick Check:

    Lambda syntax = lambda params: expression [OK]
Hint: Lambda uses colon, no def or return [OK]
Common Mistakes:
  • Including 'def' or 'return' in lambda
  • Missing colon after parameters
  • Using parentheses incorrectly
3. What is the output of the following code?
add = lambda x, y: x + y
def add_func(x, y):
    return x + y

print(add(3, 4))
print(add_func(3, 4))
medium
A. 7\n7
B. 34\n34
C. TypeError\nTypeError
D. None\nNone

Solution

  1. Step 1: Understand lambda and regular function behavior

    Both add (lambda) and add_func (regular) add two numbers and return the sum.
  2. Step 2: Evaluate the print statements

    Calling add(3, 4) and add_func(3, 4) both return 7, so output is two lines with 7.
  3. Final Answer:

    7 7 -> Option A
  4. Quick Check:

    Both add functions return sum = 7 [OK]
Hint: Both lambda and def return same result if logic matches [OK]
Common Mistakes:
  • Thinking lambda returns string concatenation
  • Confusing output with error messages
  • Assuming lambda can't return values
4. Identify the error in this code snippet:
multiply = lambda x, y:
    x * y

print(multiply(2, 3))
medium
A. No error; output is 6
B. SyntaxError due to missing colon after lambda parameters
C. TypeError because lambda cannot take two arguments
D. IndentationError because lambda body is on new line

Solution

  1. Step 1: Check lambda syntax rules

    Lambda functions must have their expression on the same line as the lambda keyword and parameters.
  2. Step 2: Analyze the code structure

    The code places the expression x * y on the next line, causing an IndentationError.
  3. Final Answer:

    IndentationError because lambda body is on new line -> Option D
  4. Quick Check:

    Lambda body must be on same line [OK]
Hint: Lambda body must be on same line as parameters [OK]
Common Mistakes:
  • Placing lambda body on next line
  • Adding colon after lambda parameters
  • Thinking lambda can't take multiple arguments
5. You want to sort a list of tuples by the second item using a function. Which is the best way to write the key function?
hard
A. list.sort(key=lambda t: t[1])
B. def get_second(t): return t[1] list.sort(key=get_second(t))
C. list.sort(key=lambda t t[1])
D. list.sort(key=get_second())

Solution

  1. Step 1: Understand sorting with key functions

    The key parameter expects a function that takes one argument and returns the value to sort by.
  2. Step 2: Evaluate options for correctness and efficiency

    def get_second(t): return t[1] list.sort(key=get_second(t)) defines a regular function but incorrectly calls get_second(t) (causing NameError: 't' not defined). list.sort(key=lambda t: t[1]) uses a lambda inline, which is concise and common. list.sort(key=lambda t t[1]) has syntax error (missing colon). list.sort(key=get_second()) calls get_second() instead of passing the function.
  3. Step 3: Choose best practice

    Using a lambda inline is best for simple, one-time use functions.
  4. Final Answer:

    list.sort(key=lambda t: t[1]) -> Option A
  5. Quick Check:

    Lambda inline for simple key function [OK]
Hint: Use lambda inline for simple sort keys [OK]
Common Mistakes:
  • Forgetting colon in lambda
  • Calling function instead of passing it
  • Using multi-line def when lambda suffices