Find sum of even number in python by R4R Team

Example-

list is l=[1,2,1,2,1,2]
then sum of even number from the list is 2+2+2=6


program-

#import the functools module
import functools

l=[1,2,3,4,5,6]
print("List is ",l)

#first way
sum=0
for i in l:
if i%2==0:
sum+=i
print("Sum of even term is :",sum)

#second way
l=filter(lambda x: x%2==0,l )
sum=functools.reduce(lambda x,y:x+y,l)
print("Sum of even term is :",sum)


output-

List is [1, 2, 3, 4, 5, 6]
Sum of even term is : 12
Sum of even term is : 12


-In this program, we have a list [1,2,3,4,5,6], then we find the sum of even number from the list.




Leave a Comment:
Search
Categories
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!