what is lambda function?

Python Lambda function is known as the anonymous function that is defined without a name. Python allows us to not declare the function in the standard manner, i.e., by using the def keyword. Rather, the anonymous functions are declared by using the lambda keyword. However, Lambda functions can accept any number of arguments, but they can return only one value in the form of expression.

File name : index.py

# a is an argument and a+10 is an expression which got evaluated and returned.
x = lambda a:a+10
# Here we are printing the function object
print(x)
print(x(5))
print("sum = ",x(20))

output :-

<function <lambda> at 0x2b185b68d1f0>
15
sum = 30

Example

Multiply argument x with argument y and return the result:

File name : index.py

mul = lambda x, y: x * y
print(mul(5, 6))

Output :-

30

Example :-

Summarize argument a, b, and c and return the result:

File name : index.py

x = lambda a, b, c : a + b + c
print(x(5, 6, 2))
Output :-

13





Previous Next


Trending Tutorials




Review & Rating

0.0 / 5

0 Review

5
(0)

4
(0)

3
(0)

2
(0)

1
(0)

Write Review Here