Lambda in Python

Eric Cusick
1 min readJul 11, 2021

There is an anonymous function called the lambda in python, meaning that it performs as a function without a name. To define the function using lambda is using the lambda as the keyword. Lambda is useful if one wants to quickly input a function in the code where a function is needed. We will look at an example to see the difference between a lambda and a def function.

Normal Function:

# Defining a Function
def squared(x):
return x**2
print(squared(3)) 9

Lambda:

# Lambda Expression
lambda_squared = lambda x: x**2
print(lambda_squared(3))9

With the normal function, you would need to define the function and input the parameter(s). As well as inputting the return statement to spit out the result. Whereas the lambda does not need a return statement, due to it assuming that it is returning. Despite the example given, lambda does not need to be assigned to a variable. It is quick to use and can be implemented wherever it is needed.

--

--