Python Lambda

A Lambda is a single-line function with no name. A lambda function can have any number of arguments but only one expression. The result is returned after the expression is executed. This lambda function is not declared using the def keyword.

Syntax

lambda arguments : expression

Example

Add two values and return the result:


x = lambda x, y : x + y
print(x(10, 20))
  
Output
30
Example

Multiply two values and return the result:


x = lambda x, y : x * y
print(x(5, 10))
  
Output
50

Why use Lambda Function?

A lambda function is useful when we are required to use an anonymous temporary function within another function.

Example

def my_function(y):
    return lambda x : x * y

my_multiplier = my_function(10)
print(my_multiplier(5))
  
Output
50