A lambda function is an unnamed
function with a small size. As we already know that the def keyword is used to
define a normal function in Python. Similarly, the lambda keyword is used to
define an anonymous function in Python.
A lambda function can take any
number of parameters, but only one expression can be used.
Syntax
lambda arguments : expression |
- This function accepts any number of inputs but only evaluates and returns one expression.
- Lambda functions can be used whenever function objects are necessary.
- It's important to remember that lambda functions are syntactically limited to a single expression.
- Apart from other forms of expressions in functions, it has a variety of applications in certain domains of programming.
Example:
#Write a Program Lambda Function use only one Argument
with one Expression a = lambda x : x + 10 print(‘The Value of Expression with one
Argument is =’a(5)) |
Output: The Value of Expression with one Argument is =15 |
Example:
#Write a Program Lambda Function use Two Argument with
one Expression a = lambda x, y : x * y print(‘The Value of Expression with two
Argument is =’, a(5, 6)) |
Output: The Value of Expression with two Argument is =30 |
Note: we can use more than one
argument list separated by comma (,).