Abstract:Today I’m going to talk to you about lambda functions, which in Python are commonly referred to as expressions.

This article to share from huawei cloud community “from scratch to learn python | what is Lambda functions and how to use them?” , the original author is Yuchuan.

A name is a convention for referring to or addressing any entity. Almost everything around us has a name. The same goes for programming. But does it have to be named? Or can you own “anonymous” goods? The answer is yes. Python provides Lambda functions, also known as anonymous functions, which are actually nameless. So, let’s continue with these Python “anonymous mysteries” in the following order.

  • Why are Python Lambda Functions required?
  • What are Python Lambda Functions?
  • How to write Lambda Functions?
  • How do Anonymous functions reduce the size of the code?
  • Python Lambda functions within user defined functions
  • How to use Anonymous functions within:

    filter()

    map()

    reduce()

So let’s get started 🙂

Why use Python Lambda functions?

The main purpose of anonymity becomes apparent when you only need to use something once. You can create them wherever you need them. For this reason, Python Lambda functions are also called throw functions and are used with other predefined functions such as filter (), map (), and so on. Compared to normal Python functions.

To prove this, let’s move on to Python Lambda functions.

What are Python Lambda functions?

Python Lambda functions are functions that don’t have any names. They are also known as anonymous or anonymous functions. The word “lambda” isn’t a name, it’s a keyword. This keyword specifies that the following function is anonymous.

Now that you know what these anonymous functions are, let’s take a closer look at how to write these Python Lambda functions.

How do I write Lambda functions in Python?

Use the lambda operator to create a lambda function with the following syntax:

Syntax:

Lambda argument: expression

Python lambda functions can take any number of arguments, but only one expression is required. The input or argument can start at 0 and can reach any limit. Just like any other function, it’s nice to have lambda functions with no input. Therefore, you can use lambda functions in any of the following formats:

Example:

Lambda: “specify a purpose”

In this case, lambda functions do not take any arguments.

Example:

Lamda a 1: “designated use of 1”

In this case, lambda takes one input, which is 1.

Similarly, you can have lambda a 1, a 2, a 3.. A n.

Let’s take a few examples to illustrate this point:

Example 1:

a = lambda x: x*x
print(a(3))

Output: 9

Example 2:

A = lambda x,y: x*y print(a(3,7))

Output: 21

As you can see, I’ve given two examples here. The first example uses only a lambda function with a single expression, while the second example has two arguments passed to it. Note that both functions have an expression followed by an argument. Therefore, lambda functions cannot be used where multiline expressions are required.

On the other hand, a normal Python function can use any number of statements in its function definition.

How do anonymous functions reduce code size?

Before comparing the amount of code required, let’s first write down the syntax of the regular function and then compare it to the syntax of the lambda function described earlier.

Any regular function in Python is defined using the def keyword, as follows:

Syntax:

Def function_name (parameters) :

statements

As you can see, lambda functions require much less code than normal functions.

Let’s rewrite the previous example with today’s normal functions.

Example:

def my_func(x):
    return x*x
print(my_func(3))

Output: 9

As you can see, in the above example, we need to use the return statement in my_func to calculate the square value of 3. In contrast, lambda functions do not use this return statement. Instead, the body of the anonymous function is written on the same line as the colon followed by the function itself. Therefore, the size of the function is smaller than the size of my_func.

However, the lambda function in the above example is called using some other variable a. This is done because these functions are nameless and therefore need to call certain names. But the fact that you need to actually assign other names to call them, why use such nameless functions, seems confusing? Of course, after assigning a name to my function, it is no longer nameless! The correct?

This is a reasonable question, but the point is that this is not the right way to use these anonymous functions.

Anonymous functions are best used in other higher-order functions that take functions as arguments or return functions as outputs. To prove this point, let’s now move on to the next topic.

Python Lambda functions in user-defined functions:

As mentioned above, lambda functions can be used in other functions to mark the greatest advantage.

The following example consists of new_func, a regular Python function that takes a single parameter x. This parameter is then added to the unknown parameter y provided through the lambda function.

Example:

def new_func(x):
    return(lambda y: x+y)
t=new_func(3)
u=new_func(2)
print(t(3))
print(u(3))

Output:

June 5

As you can see, in the above example, whenever we use new_func (), we call the lambda function that exists in new_func. Each time, we can pass individual values to the parameters.

Now that you’ve seen how to use anonymous functions in higher-order functions, let’s move on to one of the most popular uses of anonymous functions is in the filter (), map (), and reduce () methods.

How to use anonymous functions in filter (), map (), and reduce () :

The anonymous function in filter () :

Screening () :

The filter () method is used to filter the given iterables (lists, collections, and so on), with the help of another function passed as an argument, to test whether all elements are true or false.

The syntax for this function is:

Syntax:

Filters (functions, iterable)

Now consider the following example:

Example:

My_list = [2,3,4,5,6,7,8] new_list = list(filter(lambda a: (a/3 == 2), my_list))

Output: [6]

Here, my_list is a list of iterable values that are passed to the filter function. This function uses the lambda function to check if there are any values in the list (divided by 3 equals 2). The output consists of a list of expressions that satisfy the existence of an anonymous function.

Map () :

The map () function in Python is a function that applies a given function to all iterable objects and returns a new list.

Syntax:

Map (function, iterable)

Let’s take an example to demonstrate the use of the lambda function in the map () function:

Example:

My_list = [2,3,4,5,6,7,8] new_list = list(map(lambda a: (a/3! = 2), li)) print(new_list)

Bold output:

[True, True, True, True, True, False, True, True] The output above shows that as long as the value of the iterable variable is divided by 3, it does not equal 2, and the result should be True. Therefore, for all elements in my_list, when the condition changes to False, it will return true (except for the value 6).

Reduce () :

The reduce () function is used to apply some other function to the list of elements passed to it as an argument, and ultimately returns a single value.

The syntax for this function is as follows:

Syntax:

To reduce (function, order).

Example:

From functools import reduce reduce(lambda A, B: A + B, [23,21,45,98])

The following diagram depicts the above example:

Output: 187

The output clearly shows that all the elements of the list have been added consecutively to return the final result.

Click on the attention, the first time to understand Huawei cloud fresh technology ~