Python Lambda function

Lambda functions (also called anonymous functions) are tiny functions without a name, only an expression as its body.

inPythonThey are usinglambdaKey words:

lambda <arguments> : <expression>

The body must be a single expression. Expression, not statement.

This difference is important. The expression returns a value, but the statement does not.

The simplest example of a lambda function is a function that doubles the value of a number:

lambda num : num * 2

Lambda functions can accept more parameters:

lambda a, b : a * b

Lambda functions cannot be called directly, but you can assign them to variables:

multiply = lambda a, b : a * b

print(multiply(2, 2)) # 4

When combined with other Python features (for example, withmap(),filter()withreduce().


More python tutorials: