Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

How to use Lambda Function in Python?

In the following code, we are assigning a function to a variable 'add'

add = lambda x, y: x + y
print(add(4, 6))

What is a lambda function in python?


1 Answer
Rajendra Dharmkar

These are basically anonymous one-line functions created at runtime which are not bound to the name of the functions.

They return the definition of the function on the fly.

Lambda functions don't contain a return statement, they always return an expression.

You can always put a lambda definition anywhere a function is expected.

Suppose we have a function which is to be used only once and called from only one place, then we can use lambda functions.

So you don't need to give it a name and you can define the functionality there itself. Hence, we eliminate the use of a function and use Lambda expression.

SYNTAX :

lambda argument: manipulate(argument)

The given code defines a lambda function that gives the following output

add = lambda x, y: x + y
print(add(4, 6))

OUTPUT

10
Advertisements

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.