What are Lambda expressions in python?
Lambda expressions:
- Lambda functions are single-expression functions that are not necessarily bound to a name.
- Lambda expression is an anonymous function.
- Executing a lambda function evaluates its expression and then automatically returns its result. So there is always an implicit return statement.
- It's a shorthand that allows us to write a method in the same place we are going to use it.
- The difference between lambdas and regular function definitions is that lambda functions are restricted to a single expression. This means a lambda function can't use statements or annotations, not even a return statement.
- Example:
double = lambda x: x * 2
double(2) # it will return 4
Comments
Post a Comment