Anonymous Function in Python/ Lambda Function

 

Anonymous Function in Python/ Lambda Function


What is Anonymous Function in Python or  Lambda Function in Python:

When function has no name when it is defined is called anonymous function. In Python we can use lambda keyword to define anonymous function instead of the def keyword.  Another name of Lambda function is anonymous function in Python. The general syntax to create lambda function is as follows

How to declare anonymous function:

lambda arguments: expression

Lambda function have only one expression. Arguments are comma separated list of parameters. Lambda function can take as many arguments as you want.  This function is written in single line of code.  The expression is a statement which is evaluated and return the value to lambda function.

For example,

area=lambda r:3.14*r*r

result=area(5.5)

print("The area of circle is:",result)

Output:

The area of circle is: 94.985

In above example we defined lambda function area to calculate area of circle. Lambda function is defined with one argument r which takes radius. In the lambda function one processing statement is written to calculate area. Lambda function is called with 5.5 radius value. The function call is stored in result variable and the to display result this result variable is displayed with the help of print function.

Another Example,

r=int(input("Enter radius:"))

area=lambda r: print("The area of circle is:",3.14*r*r)

area(r)

Output:

Enter radius:5

The area of circle is: 78.5

In this example radius is stored in r variable and this argument is used as lambda function argument. Lambda function expression is display statement in which processing statement is written. Lambda function area is called with argument r.

Another example,

# Lambda with three arguments to perform addition of three numbers

n1=int(input("Enter first number:"))

n2=int(input("Enter second number:"))

n3=int(input("Enter third number:"))

addition=lambda n1,n2,n3:n1+n2+n3

print("The addition is:",addition(n1,n2,n3))

Output:

Enter first number:100

Enter second number:300

Enter third number:600

The addition is: 1000

Here lambda function with addition is defined to perform addition of three numbers.

 

How to Use Conditional Statement in Lambda Function:?

largest=lambda a,b: "a is greater" if(a>b) else "b is greater"

result=largest(10,30)

print(result)

Output:

b is greater

map() Method in Lambda Function:

map() function is used to apply a function to each element in a sequence such as tuple, list or dictionary.

# map() in lambda

list1=[2,5,10,15]

print("The original list is:", list1)

list_square=map(lambda n: n**2,list1)

print("The squared list is:",end=" ")

print(list(list_square))

Output:

The original list is: [2, 5, 10, 15]

The squared list is: [4, 25, 100, 225]

Here map() is used to apply lambda function to each one of the list items. This function takes one argument n and it returns the square of each one of the items from list1. The result of this method is new iterable that contains squares of the original list. List() is used to convert the list iterable into new list.

 

Filter() in Lambda Function:

Filter() is used to filter out the elements from sequence such as tuple, list or dictionary by using certain conditions. The method returns a iterable sequence with the elements that satisfy the condition.

For example,

# filter function in lambda

list1=[2,7,9,10,2,4,6]

print("The original list is:",list1)

evenlist=filter(lambda n: n%2==0, list1)

print("The evenlist is:",list(evenlist))

Output:

The original list is: [2, 7, 9, 10, 2, 4, 6]

The evenlist is: [2, 10, 2, 4, 6]

In this example filter() method is used to filter out only even numbers from given list. 

टिप्पणी पोस्ट करा

0 टिप्पण्या