How to use Lambda Function in Python?

PythonServer Side ProgrammingProgramming

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)

Example

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

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

Output

10
raja
Published on 07-Dec-2017 10:47:42
Advertisements