0
0
Pythonprogramming~10 mins

Why lambda functions are used in Python - Test Your Understanding

Choose your learning style9 modes available
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.