Anonymous
Functions
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().
Note
filter(), map() and reduce() functions are
beyond the scope of this book
The syntax for anonymous functions is as
follows:
lambda
[argument(s)] :expression
Example:
sum = lambda
arg1, arg2: arg1 + arg2
print ('The Sum is :',
sum(30,40))
print ('The Sum is :',
sum(-30,40))
Output:
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().
Related Topics
Privacy Policy, Terms and Conditions, DMCA Policy and Compliant
Copyright © 2018-2023 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.