Python Lambda Functions

Lambda Function is an anonymous function. It does not have a name. But, it can take arguments, and return a value.

Lambda Functions are useful when we need a function that can take some arguments and return a value, but need not be defined as a regular function.

Syntax

The syntax of a lambda function is

</>
Copy
lambda arguments: expression

where lambda is the keyword, arguments are the inputs that this lambda shall receive, and expression is evaluated and returned.

This lambda function can be assigned to a variable, and the variable can be used as a function.

</>
Copy
x = lambda arguments: expression

Examples

Lambda Function that Increments given Value

In the following example, we define a lambda function that can take an argument, increment the value by 5, and return the value.

Python Program

</>
Copy
x = lambda a: a + 5
output = x(24)
print('Result :', output)

Output

Result : 29

Lambda Function for Addition

In the following example, we define a lambda function that can take two arguments, and return their sum.

Python Program

</>
Copy
x = lambda a, b: a + b
output = x(3, 5)
print('Sum :', output)

Output

Sum : 8

Conclusion

In this Python Tutorial, we learned about Lambda Functions, their syntax, and how to use them in a program, with examples.