Lambda Function in Python
In Python, the normal functions are defined applying the def keyword. Now want to know about anonymous function in Python? A lambda function in Python is a tiny, anonymous function. An anonymous function here is nothing but a function that doesn’t have a name.
The lambda function can take n number of arguments but can consist of only a single expression.
Syntax of Lambda Function
The lambda function can consist of any number of arguments but can have only one expression which is assessed and later returned. These functions are limited to a single expression in terms of syntax, and they can’t even use a return statement. One should also remember that the lambda functions can be applied wherever function objects are needed. Generally, you can use it when you need an anonymous function for a short span.
The general syntax of a lambda function is quite simple:
lambda argument_list: expression
The argument list consists of a comma separated list of arguments and the expression is an arithmetic expression using these arguments. You can assign the function to a variable to give it a name.
The following example of a lambda function returns the sum of its two arguments:
>>> sum = lambda x, y : x + y