Lambda functions in Python are small, anonymous functions defined using the lambda keyword.
They can take any number of arguments but can only have one expression.
The expression is evaluated and returned.
*Lambda functions are often used for short, simple operations where a full function definition is not necessary.
*
So question arises what is anonymous in this statement?
-
A normal function in Python is usually defined using def and has a name:
def add(x, y): return x + y
-
A lambda function is defined using the lambda keyword and is typically not given a name:
lambda x, y: x + y
-
If you assign a lambda to a variable, you're still not giving the function itself a formal name like with def:
add = lambda x, y: x + y
Explanation:-
As it is written in one, not like function with two line. It needed sometimes because to make code clean and optimised.
Top comments (0)