Home | | Computer Science 12th Std | Anonymous Functions

Python - Anonymous Functions | 12th Computer Science : Chapter 7 : Core Python : Python Functions

Chapter: 12th Computer Science : Chapter 7 : Core Python : Python Functions

Anonymous Functions

In Python, anonymous function is a function that is defined without a name.

Anonymous Functions

What is anonymous function?

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.

What is the use of lambda or anonymous function?

·        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

 

Syntax of Anonymous Functions

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().

 

Tags : Python , 12th Computer Science : Chapter 7 : Core Python : Python Functions
Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail
12th Computer Science : Chapter 7 : Core Python : Python Functions : Anonymous Functions | Python


Privacy Policy, Terms and Conditions, DMCA Policy and Compliant

Copyright © 2018-2023 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.