Python Anonymous/Lambda Function
What are lambda functions in Python?
In Python, an anonymous function is a function that is defined without a name.
While normal functions are defined using the def keyword, in Python anonymous functions are defined using the lambda keyword. Hence, anonymous functions are also called lambda functions.
How to use lambda Functions in Python?
A lambda function in python has the following syntax.
lambda arguments: expression
* Lambda functions can have any number of arguments but only one expression.
* The expression is evaluated and returned.
* Lambda functions can be used wherever function objects are required.
Examples of Lambda Function in python
Example:1
Example of a Lambda function that doubles the given value.
# Program to show the use of lambda functions #method:1 double = lambda x: x * 2 print(double(5)) #method:2 print((lambda x: x * 2)(5))
Output:
10
10
>>>
In the above program, lambda x: x * 2 is the lambda function.
Here
x is the argument and
x * 2 is the expression that gets evaluated and returned.
This function has no name. It returns a function object which is assigned to the identifier double. We can now call it as a normal function. The statement
double = lambda x: x * 2
is nearly the same as
def double(x):
return x * 2
Example:2
Python program to calculate the square of a number
Sol:
#method:1 square= lambda x: x ** 2 print(square(5)) #method:2 print((lambda x: x ** 2)(5))
Output:
25
25
>>>
Example:3
Python program to calculate square of a number entered by the user.
Sol:
#method:1 n=int(input("Enter any no ")) square= lambda x: x ** 2 print(square(n)) #method:2 n=int(input("Enter any no ")) print((lambda x: x ** 2)(n))
Output:
Enter any no 5
25
Enter any no 6
36
>>>
Example:4
Python program to calculate cube of a number
Sol:
#method:1 cube= lambda x: x ** 3 print(cube(5)) #method:2 print((lambda x: x ** 3)(5))
Output:
125
>>>
Example:5
Python program to calculate cube of a number entered by the user.
Sol:
#method:1 n=int(input("Enter any no ")) cube= lambda x: x ** 3 print(cube(n)) #method:2 n=int(input("Enter any no ")) print((lambda x: x ** 3)(n))
Output:
Enter any no 10
1000
>>>
Example:6
A lambda function that adds 10 to the number passed in as an argument, and prints the result:
Sol:
#method:1 #without user input x = lambda a : a + 10 print(x(5)) #using user input n=int(input("Enter any no ")) x = lambda a : a + 10 print(x(n)) #method:2 #without user input print((lambda a : a + 10)(100)) #using user input n=int(input("Enter any no ")) print((lambda a : a + 10)(n))
Output:
15
Enter any no 25
35
110
Enter any no 256
266
>>>
Example:7
A lambda function that multiplies arguments “a” and “b” and prints the result:
Sol:
#method:1 #without user input x = lambda a, b : a * b print(x(2, 5)) #using user input n1=int(input("Enter 1st no ")) n2=int(input("Enter 2nd no ")) x = lambda a, b : a * b print(x(n1, n2)) #method:2 #without user input print((lambda a, b : a * b)(2,5)) #using user input n1=int(input("Enter 1st no ")) n2=int(input("Enter 2nd no ")) print((lambda a, b : a * b)(n1,n2))
Output:
10
Enter 1st no 2
Enter 1st no 33
66
10
Enter 1st no 2
Enter 1st no 6
12
>>>
Example:8
A lambda function that calculates the sum of three arguments and prints the result:
Sol:
#method:1 #without user input x = lambda a, b,c : a+b+c print(x(2, 5,10)) #using user input n1=int(input("Enter 1st no ")) n2=int(input("Enter 2nd no ")) n3=int(input("Enter 3rd no ")) x = lambda a, b,c : a + b + c print(x(n1, n2,n3)) #method:2 #without user input print((lambda a, b,c : a + b + c)(10,20,30)) #using user input n1=int(input("Enter 1st no ")) n2=int(input("Enter 2nd no ")) n3=int(input("Enter 3rd no ")) print((lambda a, b,c : a + b +c)(n1,n2,n3))
Output:
17
Enter 1st no 2
Enter 2nd no 3
Enter 3rd no 6
11
60
Enter 1st no 10
Enter 2nd no 25
Enter 3rd no 6
41
>>>
Next : Why Use Lambda Functions?
Tutorials | Technical Questions and Important programs | Interview Questions |
---|---|---|
C Programming C++ Programming Basic Python Tutorial Advanced Python Tutorial |
C Language C++ Programming Python programming C Important Programs |
C Interview Questions C++ Interview Questions Python Interview Questions HTML Interview Questions |