Sitemap

Lambda function in Python

Feb 22, 2023

--

A lambda function in Python is a small anonymous function that begins with the lambda keyword followed by a comma-separated parameter list, a colon (:) and an expression.

Get Hadi’s stories in your inbox

Join Medium for free to get updates from this writer.

A lambda implicitly returns its expression’s value. So any simple function of the form

def function_name(parameter_list):
return expression

may be expressed as a more concise lambda of the form

lambda parameter_list: expression

For example, consider the following lambda function:

multiply = lambda x, y: x * y
result = multiply(3, 4)
print(result) # Output: 12

This lambda function takes two arguments x and y and returns their product. We can call this lambda function like any other function.

--

--

Hadi
Hadi

Written by Hadi

Software engineering student skilled in Python. Passionate about AI/ML and determined to make a positive impact in tech.