Lambda expressions are also known as anonymous functions. Learn how and when to use them

In Python, a lambda function is an anonymous function (a function without a name) that can take any number of arguments but only contains a single expression. As an example, here is a lambda that multiplies a number by three:

lambda x : x * 3

This lambda isn’t particularly useful as-is because now there’s no way to call it. However, you can assign it to a variable and then call it just like a regular function:

mult3 = lambda x : x * 3
mult3(15.0) ## returns 45

You don’t need to assign a lambda to a variable. Instead, you can call it right away like this:

(lambda x : x * 3)(15.0) ## returns 45.0

Lambda functions are especially useful when the functionality is needed for only a short period of time and you don’t want to waste resources by creating a separate method to do the job.

#python #programming #software-development #data-science

Understand Python Lambda Expressions in 3 Minutes
3.25 GEEK