Home > Backend Development > Python Tutorial > Comprehensive analysis of Lambda expressions in Python

Comprehensive analysis of Lambda expressions in Python

WBOY
Release: 2016-12-05 13:27:19
Original
1536 people have browsed it

What is a Lambda expression

"Lambda expression" (lambda expression) is an anonymous function. Lambda expression is named based on the lambda calculus in mathematics. It directly corresponds to the lambda abstraction (lambda abstraction). It is an anonymous function, that is, a function without a function name. . Lambda expressions can represent closures (note that they are different from the traditional mathematical sense).

Lambda is an anonymous function. When we need to call a certain function repeatedly and don’t want to write so much code, we can use lambda expressions instead.

Universal format of lambda:

lambda argument: manipulate(argument)

Sample code:

add = lambda x,y : x + y
print add(3,5)
#output: 8

Usage:

Sort.

a = [(1, 2), (4, 1), (9, 10), (13, -3)]
a.sort(key=lambda x: x[1])
print(a)
# Output: [(13, -3), (4, 1), (1, 2), (9, 10)]
Copy after login

The above is the Lambda expression in Python introduced by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. I would also like to thank you all for your support of the Script House website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template