function is so called function because is not declare with def keyword like other
functions.Rather , they are created using lambda keyword .Lambda functions are
Syntax of Lambda function can be written on single line .
i.e. :
lambda arguments :expression
multiple arguments must be separated by an comma " , " .
Lambda function is assigned to a variable to give name to it .
EXAMPLE :
multi=lambda a, b : a*b
print("Multiplication =",multi (6,4))
OUTPUT : Multiplication = 24
In the above code ,the lambda function returns the multiplication of its two
arguments. In above program ,lambda a , b: a*b: is lambda function ,a,b are
formal parameters and a*b is expression that get evaluated first and then returned
NOTE
|
Lambda function has no name .
|
lambda a, b :a*b
is similar to
def multi (a, b):
return a*b
So it make sense to use lambda function for small function to reduce code
length , user can use lambda function whenever function objects are required
NOTE
|
Lambda functions in python are not similar/equivalent to inline function in C.
|
KEY Points to Remember :
1.Lambda function has no name .
2.Lambda function can take any number of arguments.
3.Lambda function will return only one value >
4.Lambda Function does not have an explicit return statement .
5.Syntax of lambda function is on one line .
6.Lambda function can not access global variables .
Users can pass lambda functions in User defined functions as well .
EXAMPLE :