Python Lambda Functions

In python, lambda functions are the functions without a name, and we can also call it anonymous functions.

 

The lambda functions are useful for sending functions as arguments to other functions and performing operations with an anonymous function name.

 

The lambda functions are not a substitute for normal functions because the lambda functions will operate on a single expression and do not contain any other statements to execute as normal functions.

Lambda Function Syntax

As discussed in the python functions article, we will use the def keyword to define the function but to define the lambda function; you need to use the lambda keyword followed by the arguments and expression.

 

Following is the syntax of defining the lambda function in python.

 

lambda argument(s): expression

The lambda function will accept any number of arguments, but it will allow having only one expression. When the lambda function is called, the expression will return the value.

Lambda Function Example

Following is the example of creating and calling the lambda function in python.

 

multiply = lambda x: x * 10
print(multiply(9))

If you observe the above example, we used the lambda keyword to create the lambda function, and we are calling the lambda function by sending an argument value.

 

When you execute the above python program, you will get the result as shown below.

 

90

If required, you can also send multiple arguments to the lambda function. Following is the example of sending multiple arguments to the lambda function.

 

sum = lambda a, b, c: a + b + c
print(sum(10, 20, 40))

If you observe the above example, we are sending multiple arguments to the lambda function to execute the defined expression (a + b + c).

 

When you execute the above python program, you will get the result as shown below.

 

70

Function with Lambda Function

In python, you can use lambda functions inside of other functions based on your requirements.

 

Following is the example of using lambda or anonymous function inside of another function in python.

 

def calculate(b):
    return lambda a: a * b
x = calculate(5)
print(x(10))

If you observe the above example, we created a lambda function inside another function (calculate) to return the value. We are calling the lambda function by sending the required arguments.

 

When you execute the above python program, you will get the result as shown below.

 

50

In lambda functions, it’s not mandatory to return the value. Following is the example of creating the lambda function without returning any value.

 

x = lambda str: print(str)
x("welcome to tutlane")

If you observe the above example, we are not returning any value from the lambda function expression. Instead, we are just printing the text.

 

When you execute the above python program, you will get the result as shown below.

 

welcome to tutlane

This is how you can use lambda or anonymous functions in python to create functions without a name based on your requirements.