0
0
Pythonprogramming~5 mins

Lambda syntax and behavior in Python - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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, which is returned automatically.
Click to reveal answer
beginner
How do you write a lambda function that adds 5 to its input?
You write it as lambda x: x + 5. This means the function takes one argument x and returns x + 5.
Click to reveal answer
beginner
Can a lambda function contain multiple expressions or statements?
No, a lambda function can only contain a single expression. It cannot have multiple statements or commands like loops or assignments.
Click to reveal answer
intermediate
How does a lambda function differ from a regular function defined with def?
A lambda function is anonymous (has no name) and is limited to one expression. A regular function can have multiple statements, a name, and more complex behavior.
Click to reveal answer
beginner
What is the output of this code?
add = lambda x, y: x + y
print(add(3, 4))
The output is 7. The lambda function adds the two inputs 3 and 4 and returns the sum.
Click to reveal answer
What keyword is used to create a lambda function in Python?
Alambda
Bdef
Cfunc
Dfunction
Which of the following is a valid lambda function syntax?
Adef lambda(x, y): return x + y
Blambda (x, y) { return x + y }
Clambda x, y: x + y
Dlambda x, y { x + y }
What will this lambda function return?
lambda x: x * 2
ADouble the input value
BHalf the input value
CAlways returns 2
DReturns the input unchanged
Can a lambda function have multiple statements inside it?
AOnly if using semicolons
BYes, multiple statements separated by commas
CYes, if enclosed in parentheses
DNo, only one expression is allowed
What is the main use of lambda functions?
ATo define classes
BTo create small anonymous functions quickly
CTo replace all regular functions
DTo write multi-line functions
Explain what a lambda function is and how it differs from a regular function defined with def.
Think about the size and naming of the function.
You got /5 concepts.
    Write a lambda function that takes two numbers and returns their product. Then explain how it works.
    Use the format: lambda arguments: expression
    You got /4 concepts.