How lambda expressions are used


Tools used:

1. Python editor

2. A computer

Before learning lambda expressions, we need to know what anonymous functions and lambda expressions are.

What is a lambda expression?

Lambda expressions are often used to create an anonymous function that requires a function but does not want to waste time naming it. Lambda expressions are simpler than ordinary functions and do not require names.

What are anonymous functions?

Anonymous functions can create a function without explicitly defining the function.

What’s the use of lambda expressions?

Let’s take an example.

If you want to make a function that can add two parameters, there are two ways.

The first method

Use def keyword

def add1(a,b):

Required code lines: 3 lines

The second method

Using lambda expressions

a = lambda a,b:a+b

Required code lines: 2 lines

From the above example, we can see that lambda expression is simpler and more convenient than def definition function.

This work adoptsCC agreement, reprint must indicate the author and the link to this article

coder Derek