In Python, lambda keyword is used to create anonymous function in Python. User defined functions in Python are defined using def keyword. However, anonymous functions in Python are defined using lambda keyword.
The syntax of anonymous function is
Contents
hide
afun=lambda parameters : expression
Lambda function expression must be in a single line expression.
Examples are-
Sum of Two Numbers using Lambda Function
>>>sum= lambda a, b: a + b
>>>print(sum(12,6)
Output would be
18
Subtraction of Numbers using Lambda Function
>>>sub= lambda a, b: a – b
>>>print(sub(18,6)
Output would be
6
Multiplication of Two Numbers using Lambda Function
>>>mul= lambda a, b: a * b
>>>print(mul(18,6)
Output would be
108
Division using Lambda Function
>>>div= lambda a, b: a / b
>>>print(div(18,6)
Output would be
2.0
Computing Square of a Number using Lambda Function
>>>square= lambda a: a * a
>>>print(square(5)
Computing Mean of three Numbers using Lambda Function
>>> mean= lambda a, b, c: (a+b+c)/3
>>> mean(3,4,5)
Output would be
4.0