Python knowledge points: lambda expressions

Hello everyone, welcome to Crossin's programming classroom!

Python is a concise language, and lambda expressions fully reflect this feature of Python.

A lambda expression can be thought of as a kind of anonymous function. It allows you to quickly define an extremely simple one-liner function. For example, a function that implements the addition of three numbers:

def sum(a, b, c):
  return a + b + c


print(sum(1, 2, 3))
print(sum(4, 5, 6))

output:

6
15

If you use a lambda expression to achieve:

sum = lambda a, b, c: a + b + c


print(sum(1, 2, 3))
print(sum(4, 5, 6))

output:

6
15

The result of both methods is the same.

Syntax format of lambda expression:

lambda 参数列表: 表达式

When defining a lambda expression, there are no parentheses around the parameter list, no return keyword before the return value, and no function name.

It is written more concisely than def. However, its body can only be an expression, not a code block, or a command (such as del). Therefore, lambda expressions lose certain functions while gaining brevity, and the logic that can be expressed is relatively limited.

A lambda expression creates a function object that can be called by assigning it to a variable, as in the example above.

Let's look at a more complicated example, using lambda expressions in def function definitions:

def fn(x):
  return lambda y: x + y


a = fn(2)
print(a(3))

output:

5

Here, the return value of the fn function is a lambda expression, which is equivalent to a function object. When fn is called with parameter 2, the result is:

lambda y: 2 + y
a = fn(2)

is equivalent to:

a = lambda y: 2 + y

So the result of a(3) is 5.

Lambda expressions are actually just a coding style, which is more pythonic. That doesn't mean you have to use it. In fact, anywhere a lambda expression can be used, it can be replaced by a normal def function definition. In some places where the same function needs to be used repeatedly, def can avoid repeatedly defining functions. Moreover, the def function is more general, which can lead to better code readability in some cases.

For methods like filter and sort that require built-in functions, lambda expressions are more appropriate. This will be introduced separately next time.

Of course, for beginners, another important function of understanding lambda expressions is to understand the code written by others.


Crossin's new book " Action on Code: Zero-Basic Learning of PYTHON Programming (CHATGPT Edition) " has been released. The book explains in detail the concepts of functions, parameters, return values, and anonymous functions.

f96815a4d140b2d2805018c5226df465.jpeg

This book strives to be easy to understand, so that zero-based "novice" who has no programming experience at all can learn Python. The content starts from the most basic steps of environment construction, and gradually goes deep into common practical applications. While explaining the knowledge points, it is equipped with corresponding code examples, so that readers can learn and practice to deepen their understanding.

The book covers Python environment construction, basic grammar, common data types, practical modules, regular expressions, object-oriented programming, multi-task programming and other knowledge points. In addition, three practical projects of crawler, GUI and game are provided.

The book also innovatively uses ChatGPT as an aid to programming learning, leading readers to explore a new mode of learning programming in the AI ​​era.

Thank you for retweeting and liking ~

Guess you like

Origin blog.csdn.net/qq_40523737/article/details/131079012
Recommended