Get the App
SLTechnology News&Howtos  ›  Development  › 

Why use lambda expressions

Shulou Source: shulou.com Published: 2022-06-02 06:42:39 10月11日 Update

This article is about why you use lambda expressions. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Preface

The syntax structure of the lambda expression:

#! / usr/bin/python3 # writable function description sum = lambda arg1, arg2: arg1 + arg2 # calls sum function print ("added value is:", sum (10,20)) print ("added value is:", sum (20,20))

Knowing lambda expression for the first time

In fact, lambda acts like a function. Here are the parameters and return values of the python function and lambda expression.

In fact, from this point of view, the lambda syntax is very concise, generally only one line can achieve certain functions.

Why use lambda expressions

Since both lambda expressions and python functions can achieve the same effect, why use lambda expressions?

a. Sometimes where the python function cannot be used, the lambda expression can still be used, and it is more convenient and concise.

b. Functions have to be named, but this is a difficult thing to do and easy to repeat, but lambda expressions are not quite a problem.

C. the lambda expression requires only one line, which enhances the readability of the code

Thank you for reading! This is the end of the article on "Why use lambda expressions". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

Tags: Expressions functions simplicity one line content more articles syntax good practical same difficult things code functions prefaces functions parameters readability places Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei Shulou Tech Info Redmi Shulou Information Docker