Python Lambda Funtion

Python Anonymous / Lambda Function

What are lambda functions in Python?

In Python, anonymous function is a function that is defined without a name.
While normal functions are defined using the def keyword, in Python anonymous functions are defined using the lambda keyword. Hence, anonymous functions are also called lambda functions.

How to use lambda Functions in Python?

A lambda function in python has the following syntax.

lambda arguments: expression

* Lambda functions can have any number of arguments but only one expression.

* The expression is evaluated and returned.

* Lambda functions can be used wherever function objects are required.


Write a python lambda function that doubles the input value.

double = lambda x: x * 2




In the above program,

lambda x: x * 2

is the lambda function.

x is the argument and
x * 2 is the expression that gets evaluated and returned.

This function has no name. It returns a function object which is assigned to the identifier double. We can now call it as a normal function. The statement

double = lambda x: x * 2

is nearly the same as

def double(x):
       return x * 2