Anonymous Function in Python

In this tutorial, we are gonna see what is an Anonymous Function in python, How it works and Some Examples on Anonymous function.

What is Anonymous Function

Anonymous Function also is known as lambda function and it is called as anonymous because this function is defined without a name.
Lamda function aka Anonymous function is defined with a keyword ‘lambda‘¬† whereas other normal functions are defined as ‘def funtion_name

Syntax :

lambda arguments: expression

usually, the lambda function is written in a single line of code. And also note that lambda function can have any number of arguments but only one expression.

How does it work :

We use lambda keyword to denote that we are using a lambda function

Lambda Function in Python

multiply = lambda a,b : a*b

print(multiply(5,6))

Output : 30

Normal Function:

def multiple(a,b):
    return a*b

print(multiple(5,4))

Output :20

Basically, both the function are doing the same job i.e multiplying 2 numbers. Since lambda function is just a single line expression it is preferred more. And also we can perform map, reduce, filter operations on lambda expression which makes it more versatile than the conventional def function.

I hope you are now able to understand what is Anonymous Function in Python.

Also see :

Leave a Reply

Your email address will not be published. Required fields are marked *