Lambda Function
Lambda function in Python is known as anonymous function, where we can not use the name in the time of declaration. Lambda function can not be define, means we can not use def keyword at the time of definition of the function. Instead of it we can us lambda keyword for declare this function.
Syntax:
variable=lambda arguments : expression
Example: Program to multiply of 2 numbers.
m=lambda x,y:x*y
a=int(input("Enter 1st number:"))
b=int(input("Enter 2nd number:"))
multi=m(a,b)
print("Multiplication of 2 numbers is:",multi)
Output:
Enter 1st number:5
Enter 2nd number:6
Multiplication of 2 numbers is: 30
>>>
Why use lambda function?
The lambda function will be used, when we need function without name for temporary time. It is basically used as a argument of another built in function like filter(), map() etc.
Example: Program to display the even numbers form the list.
lst=[10,11,12,13,14,15,16,17,18,19,20]
ev=list(filter(lambda x:(x%3==0),lst))
print(ev)
Output:
[12, 15, 18]
>>>
Informative blog!!
ReplyDeleteBest Python Online Course
Best Python Online Course Hyderabad
thank you
Delete