Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
3 views

Lambda_In_Python

Uploaded by

rajani kale
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Lambda_In_Python

Uploaded by

rajani kale
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

PYTHON

Lambda Function
Raushan Kumar
FILTER()
MAP()
REDUCE()

Raushan Kumar
https://www.linkedin.com/in/raushan-kumar-553154297/
Lambda Function in Python
Lambda Functions:
Sometimes we can declare a function without any name,
such type of nameless functions is called anonymous
functions or lambda functions.
The main purpose of anonymous function is just for
instant use (i.e. for one time usage)
Normal Function:
We can define by using def keyword.
def squareIt (n):
return n*n

Lambda Function:
We can define by using lambda keyword
lambda n: n*n
Syntax of lambda Function:
lambda argument_list : expression

Note:
By using Lambda Functions we can write very concise
code so that readability of the program will be
improved.

1
Q) Write a Program to create a Lambda Function to find
Square of given Number?
s=lambda n:n*n
print("The Square of 4 is :",s(4))
print("The Square of 5 is :",s(5))
Output
The Square of 4 is : 16
The Square of 5 is : 25

Q) Lambda Function to find Sum of 2 given Numbers


s=lambda a,b:a+b
print("The Sum of 10,20 is:",s(10,20))
print("The Sum of 100,200 is:",s(100,200))
Output
The Sum of 10,20 is: 30
The Sum of 100,200 is: 300

2
Q) Lambda Function to find biggest of given Values
s=lambda a,b:a if a>b else b
print("The Biggest of 10,20 is:",s(10,20))
print("The Biggest of 100,200 is:",s(100,200))
Output
The Biggest of 10,20 is: 20
The Biggest of 100,200 is: 200

Note:
Lambda Function internally returns expression value
and we are not required to write return statement
explicitly.
Note:
Sometimes we can pass function as argument to
another function. In such cases lambda functions are
best choice.
Note:
We can use lambda functions very commonly with
filter(), map() and reduce() functions, because these
functions expect function as argument.

3
filter() Function:
We can use filter() function to filter values from the given
sequence based on some condition.
Syntax:
filter(function,sequence)
Where Function Argument is responsible to perform
conditional check and Sequence can be List OR Tuple OR
String.
Q) Program to filter only Even Numbers from the List by
using filter() Function?
Without Lambda Function:
def isEven(x):
if x%2==0:
return True
else:
return False
l=[0,5,10,15,20,25,30]
l1=list(filter(isEven,l))
print(l1)
Output:
[0,10,20,30]

4
With Lambda Function:
l=[0,5,10,15,20,25,30]
l2=list(filter(lambda x:x%2==0,l))
print(l2)
Output:
[0,10,20,30]

map() Function:
For every element present in the given sequence, apply
some functionality and generate new element with the
required modification. For this requirement we should go
for map() function.
Eg: For every element present in the list perform double
and generate new list of doubles.
Syntax:
map(function, sequence)
The function can be applied on each element of sequence
and generates new sequence.

5
Q) WAP to double the element of a list
Without Lambda Function

# Function to double the number


def doubleIt(x):
return 2*x

l=[1,2,3,4,5]
l1=list(map(doubleIt,l))
print(l1)

Output:
[2,4,6,8,10]

With lambda Function


l=[1,2,3,4,5]
l1=list(map(lambda x:2*x,l))
print(l1)

Output:
[2,4,6,8,10]

6
Q) WAP to find the square of a given number in the list
l=[1,2,3,4,5]
l1=list(map(lambda x:x*x,l))
print(l1)

Output:
[1,4,9,16,25]

Note:
We can apply map() function on multiple lists also. But
make sure all list should have same length.
Syntax:
map(lambda x,y:x*y,l1,l2))
x is from l1 and y is from l2
Q) WAP to multiply the element of a list with the
element of another list.
l1=[1,2,3,4,5]
l2=[6,7,8,9,10]
result=list(map(lambda x,y:x*y,l1,l2))
print(result)

Output:
[6,14,24,36,50]

7
reduce() Function:
reduce() function reduces sequence of elements into a
single element by applying the specified function.
Syntax:
reduce(function,sequence)
reduce() function present in functools module and hence
we should write import statement.
Q) WAP to find the sum of elements in a list

from functools import reduce


l=[10,20,30,40,50]
list_sum=reduce(lambda x,y:x+y,l)
print(list_sum)

Output:
150

By: Raushan Kumar


Please follow for more such content:
https://www.linkedin.com/in/raushan-kumar-553154297/

You might also like