The Lambda keyword in python to define a lambda anonymous function. Form such as: Lambda parameter: expression. Lambda requires a parameter, followed only by a single expression as the body of the function, and the value of the expression is returned by the new function.
Lambda functions have the following characteristics compared to DEF-defined functions :
Lambda functions are anonymous functions, and DEF defines functions that are known as functions. Lambda creates a function object, but does not assign the function object to an identifier, and Def assigns the function object to a variable.
A lambda function is a single-line function. The body of a lambda is an expression, not a block of code. Only a finite amount of logic can be encapsulated in a lambda expression.
The lambda function, like the function defined by Def, is a Python object.
The expression part of a lambda function can only be an expression, not a statement, so a statement like if or for or print cannot be used in a lambda.
Example:
A=lambda:3print A () B=lambda X:x*2print B (2) C=lambda x,y:x+yprint C (2,3)
Operation Result:
3
4
5
Python lambda function