python lambda
时间: 2023-10-13 17:17:16 浏览: 82
Python lambda is a way to create anonymous functions that can be used wherever a function object is required. Lambda functions are defined using the keyword `lambda`, followed by one or more arguments separated by commas, then a colon, and the expression to be evaluated. The syntax for creating a lambda function is:
```
lambda arguments: expression
```
For example, a lambda function that adds two numbers can be defined as:
```
add = lambda x, y: x + y
```
The lambda function can then be called by passing arguments to it:
```
result = add(2, 3)
print(result) # Output: 5
```
Lambda functions are useful when a small function is needed for a short period of time, and it is not worth defining a named function. They are also commonly used for functional programming techniques such as map, filter, and reduce.
阅读全文