In Python, anonymous function is a function that is defined without a name. While normal functions are defined using the def keyword, in Python anonymous functions are defined using the lambda keyword. Hence, anonymous functions are also called as lambda functions.
· Lambda function is mostly used for creating small and one-time anonymous function.
· Lambda functions are mainly used in combination with the functions like filter(), map() and reduce().
filter(), map() and reduce() functions are beyond the scope of this book
The syntax for anonymous functions is as follows:
lambda [argument(s)] :expression
sum = lambda arg1, arg2: arg1 + arg2
print ('The Sum is :', sum(30,40))
print ('The Sum is :', sum(-30,40))
The Sum is : 70
The Sum is : 10
The above lambda function that adds argument arg1 with argument arg2 and stores the result in the variable sum. The result is displayed using the print().