Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

What is lambda in Python?

Lambda creates simple anonymous function. It is simple because it can only have one statement in its body. It is anonymous because usually it does not have a name.

The usual use is as we saw earlier when we passed it as a parameter to the map function. However, in the next example we show that you can assign the lambda-function to a name and then you could used that name just as any other function you would define using def.

def dbl(n):
    return 2*n
print(dbl(3))

double = lambda n: 2*n
print(double(3))

Output:

6
6