lambda is a frequently used keyword in the Python programming language. So, what is a lambda? What are its uses? There are tons of articles on the Internet, but there are not many articles that explain this thoroughly. Here, we have summarized "one syntax, three features, and four usages" about lambda in Python by reading information from all parties.
A syntax(recommended learning: Python video tutorial)
In Python, The syntax of lambda is unique. Its form is as follows:
lambda argument_list: expression
Among them, lambda is a keyword reserved by Python, and argument_list and expression are customized by the user. The details are as follows.
The argument_list here is the parameter list, and its structure is the same as the parameter list of the function in Python. Specifically, argument_list can have many forms.
The expression here is an expression about parameters. The parameters appearing in the expression need to be defined in argument_list, and the expression can only be a single line. 3. The lambda argument_list: expression here represents a function. This function is called lambda function.
Three characteristics
The lambda function has the following characteristics:
The lambda function is anonymous: the so-called anonymous function, in layman’s terms, is Function without name. Lambda functions have no names.
The lambda function has input and output: the input is the value passed into the parameter list argument_list, and the output is the value calculated based on the expression expression.
The lambda function generally has simple functions: a single line of expression determines that the lambda function cannot complete complex logic and can only complete very simple functions. Because the functions it implements are clear at a glance, it doesn't even need a special name to describe it.
Four usages
Since lambda syntax is fixed, there is essentially only one usage, which is to define a lambda function. In practice, depending on the application scenario of the lambda function, the usage of the lambda function can be expanded to the following:
Assign the lambda function to a variable, and call the lambda function indirectly through this variable.
Assign the lambda function to other functions, thereby replacing other functions with the lambda function.
Return the lambda function to the caller as the return value of other functions.
Pass lambda functions as parameters to other functions.
For more Python related technical articles, please visit the Python Tutorial column to learn!
The above is the detailed content of What does lambda mean in python. For more information, please follow other related articles on the PHP Chinese website!