Python Lambda:
A lambda function is a small anonymous function, it can take any number of arguments, but can only have one expression.
Syntax:
lambda arguments : expression
Example:
x = lambda a : a + 5
print(x(5))
print(x(5))
Output:
10
Note: Lambda functions can take any number of arguments.
With Lambda function we can do any operation on any number of operand.
Example:
x = lambda a, b, c : a * b * c
print(x(1, 2, 3))
Output:
6
Use Lambda Functions:
The power of lambda is better shown when you use them as an anonymous function inside another function.
Example:
def myfunc(n):
return lambda a : a * n
mydouble = myfunc(2)
print(mydouble(9))
return lambda a : a * n
mydouble = myfunc(2)
print(mydouble(9))
Output:
18

0 Comments