Definition of a lambda function
The lambda function is a commonly used built-in function in Python, also known as an anonymous function. Compared to ordinary functions, it only has a function body, omitting def and return, making the structure look more streamlined. Its basic calling syntax is as follows:
lambda [var1 [,var2,…varn]]:expression
[var1 [,var2,...varn]]: formal parameter, which can be interpreted as an input parameter for use in expressions.
expression: a function expression whose result is the return value of the lambda function.
Lambda Functions in Python
A lambda function is an anonymous function (i.e., not defined by name) that can take any number of arguments, but unlike a normal function, it only evaluates and returns an expression
The lambda function in Python is expressed using the following syntax:
lambda parameter: expression
The lambda function consists of three elements:
- Keyword lambda: similar to def in normal functions
- Arguments: support passing positional and keyword arguments, as with normal functions
- Main article: dealing with fixed-parameter expressions
Note that, unlike ordinary functions, there is no need to enclose the arguments of a lambda function in parentheses here; if the lambda function has two or more arguments, we list them with commas
We use the lambda function to compute only a short expression (ideally, a single line) and only once, which means we won't reuse the function later. Typically, we pass lambda functions as arguments to higher-order functions (functions that take other functions as arguments), such as Python's built-in functions like filter(), map(), or reduce().
Anonymous functions, as the name suggests, have no name
Let me look at the grammar:
lambda [list]: expression
Here is a description of the parameters
"""
[list]: indicates a list of parameters.
Note: A colon is needed to distinguish between arguments and expressions
Expressions: There are many ways to express an expression, and many forms of expression.
Return value: the result of the expression value
"""
For example, the code above here
lambda x:x % n > 0
x is the parameter to be passed in, and x % n > 0 is the expression, which needs to be referenced with a colon, and the result of the computed expression is the return value. Here's an example: If you design a function to find the sum of two numbers, using the normal function approach, define it as follows:
def add(x, y): return x + y print(add(3, 4))
We see that there is only one line of expression, direct operation results in the return value, that is, if we use the anonymous function of a line of code can be completed
add = lambda x, y: x + y print(add)
Here we will write our results directly, we can see that for functions that return a single line, the use of lambda expressions can save the process of defining the function, so that the code is more concise, for functions that do not need to be reused many times, the use of lambda expressions can be released immediately after the use of the program to improve the performance of the program execution. It can also be used in conjunction with other higher-order functions such as
# -*- coding: utf-8 -*- fun1 = lambda a, b: x + y x = int(input('x=')) y = int(input('y=')) print('x+y=', fun1(x, y)) def add(a, b): print('add=', a + b) add(x, y) fun = lambda a, b: a ** 2 f = fun(2, 4) print(type(fun)) print(f)
To this article on the Python analysis of the use of anonymous functions lambda article is introduced to this, more related Python lambda content, please search for my previous articles or continue to browse the following related articles I hope you will support me in the future more!