2. Lambda Function
Lambda functions are also called anonymous
functions. An anonymous function is a function
defined without a name. As we know to define
a normal function in python, we need to use
the def keyword. But in this case of anonymous
functions, we use the lambda keyword to
define the functions.
3. The characteristics of lambda function
The lambda function can take many
arguments but can return only one
expression.
Syntactically, lambda functions are
restricted to only a single expression.
We can use them as an anonymous
function inside other functions.
Lambda function is reusable.
4. Why should we use lambda functions?
Lambda functions are used when we need a
function for a short period of time. This is
commonly used when we want to pass a
function as an argument to higher-order
functions, that is, functions that take other
functions as their arguments.
5. Basic Syntax of Lambda Function
lambda arguments : expression
Every lambda
begins with the
“lambda” keyword
A lambda can have
multiple arguments
A colon precedes
the expression
The expression always
returns a object
6. Example
double = lambda x : x * 2
print(double(5))
Output:
10
Function object that accepts and
stores the result of the expression