Python Lambda Tutorial

Python Lambda

This is a detailed tutorial of Python Lambda. Learn how to take arguments and make use of expressions to return values using this python anonymous Function.

Python Lambda Function

It is a small function that is anonymous in nature. Any number of arguments can be given to a Lambda Function but it returns the value only according to one defined expression. Lambda Functions are important as they are anonymous and can be used within other user-defined Python Functions for better productivity. We’ll find it out later in this article.

Syntax

Let’s first have a look at the basic syntax of a Lambda Function to understand how it works.

The name of the function is the variable name that is being used to store the return value of the lambda function. Therefore, the variable result is the name of the lambda function itself in the above syntax. The function definition starts with the keyword lambda followed by the arguments and then the expression is written after a semi-colon.

The value of the expression is calculated as per the provided arguments and accordingly, the resultant value is returned.

Examples

Lambda functions are helpful in different ways. Let’s have a look at the different examples given below. We’ve tried to include each different example to explain different practical usage.

Single Parameter Lambda Function

Example. In the following code, we’ve written a very simple lambda function that calculates the square of a number and returns it.

In this lambda function, we’ve defined the function to accept only one parameter, i.e. par and then in the expression we’re simply multiplying this parameter twice so that it can return the square of it. Afterward, we’re simply calling the lambda function by passing the argument like normal Python Functions.

Python Lambda Function Example

Lambda Function with Multiple Parameters

Example. This demonstrates how you can accept multiple arguments using a Lambda Function.

This lambda function takes two arguments, length and breadth and by multiplying both of these it calculates the area and returns it as shown in the following screenshot.

Python Lambda Function Multiple Parameters Example

Lambda Function For Mapping inside the map() Function

Example. In the following code snippet, we’re mapping a list in such a way that it makes use of the anonymity of the lambda function to multiply each element of the list by a number 5 and returning a new Python List. You can know more about the usage of Python Map Function here.

The argument x of the lambda function here is being passed via the map() function, for each element of the list.

Python Lambda & Map Function Example

This way you can also use the lambda function inside functions like filter() as well.

Lambda Functions Inside Other Python Functions

To better use the anonymous feature of the lambda function, these are often used inside the normal python functions. Let’s find out how they are more beneficial to use inside another function.

Example 1. Here we’re calculating the multiplication table of a number provided as an argument using the lambda function and the map() function inside a user-defined function.

Python Lambda Anonymous Function Example

Example 2. Here, we’re using the anonymous nature of the lambda function even better. Observe the code and the output carefully to better understad how it’s working.

Python Anonymous Function Example

I hope you found this guide useful. If so, do share it with others who are willing to learn Python. If you have any questions related to this article, feel free to ask us in the comments section.

And do not forget to subscribe to WTMatter!

Subscribe To WTMatter!

Receive updates of our latest articles via email. Enter your email address below to get started.

Leave a Reply

Your email address will not be published. Required fields are marked *