Python Tutorial | Python List

Python List Questions

Question:6
Python program to print total even elements in the list?
Sol:

n=[1,22,31,42,51]
p=0
for i in n:
    print(i)
    if(i%2==0):
        p+=1
print("Total even elements ",p)

Output:

1
22
31
42
51
Total even elements 2
>>>

Question:7
Python program to print total odd elements in the list?
Sol:

n=[1,22,31,42,51]
p=0
for i in n:
    print(i)
    if(i%2!=0): #if(i%2==1):
        p+=1
print("Total odd elements ",p)

Output:

1
22
31
42
51
Total odd elements 3
>>>

Question:8
Python program to print the following

* Total only +ve elements in the list
* Sum of all +ve elements in the list
* Average of all +ve elements in the list
Sol:

n=[1,2,-3,-4,5]
p=0
s=0
av=0
for i in n:
    if(i>0):
        print(i)
        s=s+i
        p+=1
print("total +ve elements ",p)
print("Sum of all +ve elements ",s)
av=s/p
print("Average of all +ve elements ",av)

Output:

1
2
5
total +ve elements 3
Sum of all +ve elements 8
Average of all +ve elements 2.6666666666666665
>>>

Question:9
Python program to print the following

* Total only even elements in the list
* Sum of all even elements in the list
* Average of all even elements in the list
Sol:

n=[1,2,3,4,5,6]
p=0
s=0
av=0
for i in n:
    if(i%2==0):
        print(i)
        s=s+i
        p+=1
print("total even elements ",p)
print("Sum of all even elements ",s)
av=s/p
print("Average of all even elements ",av)

Output:

2
4
6
total even elements 3
Sum of all even elements 12
Average of all even elements 4.0
>>>

Question:10
Python program to print the following

* Total only odd elements in the list
* Sum of all odd elements in the list
* Average of all odd elements in the list
Sol:

n=[1,2,3,4,5,6]
p=0
s=0
av=0
for i in n:
    if(i%2==1): #if(i%2!=0):
        print(i)
        s=s+i
        p+=1
print("total odd elements ",p)
print("Sum of all odd elements ",s)
av=s/p
print("Average of all odd elements ",av)

Output:

1
3
5
total odd elements 3
Sum of all odd elements 9
Average of all odd elements 3.0
>>>