Python Lambda

What is lambda in Python?

First, it is an expression, so called lambda expression. The expression evaluates to the value after the colon. Since it’s an expression, it can occur anywhere an expression can occur. For example, it can be assigned to a variable:

x=lambda a: a+2

It can appear in the return statement of a function:

def myfunc(n):
    return lambda a: a*n

Second, it is a function, an anonymous function. Since it is a function, it can can accept parameters:

x=lambda a: a+2
x(3)

 

def myfunc(n):
    return lambda a: a*n
myfunc(2)(5)

The function returns the value of the expression after the colon.

If you like my content, please consider buying me a coffee. Buy me a coffeeBuy me a coffee Thank you for your support!

Leave a Reply