Bird
Raised Fist0
Pythonprogramming~10 mins

Why lambda functions are used in Python - Test Your Understanding

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
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a lambda function that adds 5 to a number.

Python
add_five = lambda x: x [1] 5
print(add_five(10))  # Output: 15
Drag options to blanks, or click blank then click option'
A/
B-
C*
D+
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using '-' instead of '+' will subtract instead of add.
Using '*' or '/' will multiply or divide, not add.
2fill in blank
medium

Complete the code to use a lambda function with the map() function to square each number in the list.

Python
numbers = [1, 2, 3, 4]
squared = list(map(lambda x: x [1] x, numbers))
print(squared)  # Output: [1, 4, 9, 16]
Drag options to blanks, or click blank then click option'
A*
B-
C+
D/
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using '+' will add the number to itself, not square it.
Using '-' or '/' will not produce the square.
3fill in blank
hard

Fix the error in the lambda function that filters even numbers using filter().

Python
numbers = [1, 2, 3, 4, 5, 6]
even_numbers = list(filter(lambda x: x [1] 2 == 0, numbers))
print(even_numbers)  # Output: [2, 4, 6]
Drag options to blanks, or click blank then click option'
A//
B%
C**
D+
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using '//' performs floor division, not remainder.
Using '**' is exponentiation, not related to even check.
Using '+' adds numbers, not checks evenness.
4fill in blank
hard

Fill both blanks to create a dictionary comprehension that maps words to their lengths only if length is greater than 3.

Python
words = ['apple', 'bat', 'car', 'door']
lengths = {word: [1] for word in words if len(word) [2] 3}
print(lengths)  # Output: {'apple': 5, 'door': 4}
Drag options to blanks, or click blank then click option'
Alen(word)
B<=
C>
Dword
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using '<=' will include words with length 3 or less.
Using 'word' instead of len(word) will map words to themselves.
5fill in blank
hard

Fill all three blanks to create a dictionary comprehension that maps uppercase words to their lengths only if length is less than 5.

Python
words = ['tree', 'sun', 'flower', 'sky']
result = { [1]: [2] for w in words if len(w) [3] 5 }
print(result)  # Output: {'TREE': 4, 'SUN': 3, 'SKY': 3}
Drag options to blanks, or click blank then click option'
Aw.upper()
Blen(w)
C<
Dw
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using 'w' instead of w.upper() will keep words lowercase.
Using '>=' or '>' will filter wrong words.
Using 'w' instead of len(w) will map words to themselves.

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

  1. Step 1: Understand the purpose of lambda functions

    Lambda functions are designed to create small, unnamed functions quickly, usually for simple tasks.
  2. Step 2: Compare with other options

    Options A, B, and C describe uses that do not match lambda functions' purpose.
  3. Final Answer:

    To create small, unnamed functions quickly for simple tasks -> Option D
  4. 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

  1. Step 1: Recall lambda syntax in Python

    Lambda functions use the syntax: lambda parameters: expression.
  2. Step 2: Check each option

    lambda x, y: x + y matches the correct syntax. Options B, C, and D use incorrect syntax styles.
  3. Final Answer:

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

    Correct lambda syntax = lambda params: expression [OK]
Hint: Lambda syntax: lambda params: expression [OK]
Common Mistakes:
  • Using def keyword with lambda
  • Adding parentheses around parameters incorrectly
  • Using braces or arrows like other languages
3. What is the output of this code?
nums = [1, 2, 3, 4]
squared = list(map(lambda x: x**2, nums))
print(squared)
medium
A. [1, 4, 9, 16]
B. [2, 4, 6, 8]
C. [1, 2, 3, 4]
D. Error: map requires a named function

Solution

  1. Step 1: Understand the lambda inside map

    The lambda function squares each number: x**2.
  2. Step 2: Apply lambda to each element in nums

    Applying to [1, 2, 3, 4] gives [1, 4, 9, 16].
  3. Final Answer:

    [1, 4, 9, 16] -> Option A
  4. Quick Check:

    Squares of nums = [1,4,9,16] [OK]
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

  1. Step 1: Check lambda function syntax

    Lambda functions are single expressions and cannot use statements like return.
  2. Step 2: Identify the error in the code

    The code incorrectly uses return inside a lambda, which is not allowed.
  3. Final Answer:

    Lambda functions cannot use return statement -> Option C
  4. 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

  1. Step 1: Understand sorting with key parameter

    The key argument takes a function to extract the sorting value from each item.
  2. 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.
  3. Final Answer:

    data.sort(key=lambda x: x[1]) -> Option B
  4. 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]
Common Mistakes:
  • Omitting key= in sort
  • Using wrong index for tuple
  • Passing lambda directly without key