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.
The syntax of a lambda function is
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.
x = lambda arguments: expression
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.
x = lambda a: a + 5 output = x(24) print('Result :', output)Try Online
Result : 29
Lambda Function for Addition
In the following example, we define a lambda function that can take two arguments, and return their sum.
x = lambda a, b: a + b output = x(3, 5) print('Sum :', output)Try Online
Sum : 8
In this Python Tutorial, we learned about Lambda Functions, their syntax, and how to use them in a program, with examples.