Lambda

A lambda operator or lambda function is used for creating small, one-time, anonymous function objects in Python.

Basic Syntax

lambda arguments : expression

A lambda operator can have any number of arguments but can have only one expression. It cannot contain any statements and returns a function object which can be assigned to any variable.

Example

Let’s look at a function in Python:

The above function’s name is add , it expects two arguments x and y and returns their sum.

Let’s see how we can convert the above function into a lambda function:

In lambda x, y: x + y; x and y are arguments to the function and x + y is the expression that gets executed and its values are returned as output.

lambda x, y: x + y returns a function object which can be assigned to any variable, in this case, the function object is assigned to the add variable.

If we check the type of add , it is a function .