This is a simple question to test your understanding of Python Lambda Function Inside Function.
What will be the output of the following code:
def mystery_function(x):
return lambda y: x + y
result = mystery_function(5)
print(result(3))
Select an option:🤔🧐
A: 8
Here’s the explanation:
- The
mystery_function
takes an argumentx
and returns a lambda function. - The lambda function takes another argument
y
and adds it to the value ofx
that was passed to the outer function. - When you call
mystery_function(5)
, it returns a lambda function that will add 5 to any value you pass to it. - Finally, you call the returned lambda function with
result(3)
. This adds 5 to 3 and prints the result, which is8
.
So, option (a) is the correct answer.