Question 8: Lambda Function Inside Function

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:

  1. The mystery_function takes an argument x and returns a lambda function.
  2. The lambda function takes another argument y and adds it to the value of x that was passed to the outer function.
  3. When you call mystery_function(5), it returns a lambda function that will add 5 to any value you pass to it.
  4. Finally, you call the returned lambda function with result(3). This adds 5 to 3 and prints the result, which is 8.

So, option (a) is the correct answer.

Leave a Reply