In Python, a lambdafunction is a nameless function that can have arguments of any number. It is defined with a single expression that is evaluated and the result returned when the function is called.
Syntax:
lambda arguments: expression
Example:
x = lambda a : a + 15 //adding 15 to argument a and returning result
print(x(5))
#OUTPUT: 20
Lambda functions are used when a small function is needed temporarily, and defining a complete function is not necessary. They are also useful as arguments for higher-order functions like
map(), filter(), and reduce(). The lambda function takes a list of arguments separated by commas, and the expression is the code that gets executed when the function is called.
For example, here is a lambda function that takes two arguments and returns their sum:
add = lambda x, y: x + y
This lambda function can be called like any other function:
print(add(2, 3))
#OUTPUT: 5
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
In Python, a lambda function is a nameless function that can have arguments of any number. It is defined with a single expression that is evaluated and the result returned when the function is called.
Syntax:
Example:
Lambda functions are used when a small function is needed temporarily, and defining a complete function is not necessary. They are also useful as arguments for higher-order functions like map(), filter(), and reduce(). The lambda function takes a list of arguments separated by commas, and the expression is the code that gets executed when the function is called.
For example, here is a lambda function that takes two arguments and returns their sum:
This lambda function can be called like any other function: