Lambda functionsΒΆ

Some functions can be easily defined in a single line of code, and it is sometimes useful to be able to define a function “on the fly” using “lambda” notation. To define a function that returns 2*x for any input x, rather than:

def f(x):
    return 2*x

we could also define f via:

f = lambda x: 2*x

You can also define functions of more than one variable, e.g.:

g = lambda x,y: 2*(x+y)

Note

The lambda notation is one of the built-in Python keywords Python keywords, which means importing NumPy is not needed to use it.