0
0
Pythonprogramming~10 mins

Lambda vs regular functions in Python - Interactive Practice

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))
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 regular function to return the square of a number.

Python
def square(num):
    return num [1] num

print(square(4))
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 give the square.
3fill in blank
hard

Fix the error in the lambda function that should multiply two numbers.

Python
multiply = lambda a, b: a [1] b
print(multiply(3, 7))
Drag options to blanks, or click blank then click option'
A*
B+
C-
D/
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using '+' adds instead of multiplies.
Using '-' or '/' will not multiply.
4fill in blank
hard

Fill both blanks to create a dictionary comprehension that maps numbers to their squares using a lambda.

Python
square_func = lambda x: x [1] x
squares = {num: square_func(num) for num in range(1, 6) if num [2] 3}
print(squares)
Drag options to blanks, or click blank then click option'
A*
B>
C<
D+
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using '+' in lambda adds instead of squares.
Using '<' includes numbers less than 3, not greater.
5fill in blank
hard

Fill all three blanks to create a lambda that filters even numbers and returns their squares in a dictionary.

Python
is_even = lambda n: n [1] 2 == 0
square = lambda x: x [2] x
result = {num: square(num) for num in range(1, 11) if is_even(num) [3] True}
print(result)
Drag options to blanks, or click blank then click option'
A%
B*
C==
D!=
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using '!=' instead of '==' changes the condition.
Using '+' instead of '*' does not square the number.
Using '/' instead of '%' does not check evenness.