Python Tutorial | Python tuples 7

6.
Python program to traverse a tuple, calculate and display total even elements?
Sol:

#using for loop

n=(51,22,73,24,45)
p=0
for i in n:
    print(i)
    if(i%2==0):
        p+=1
print("Total even elements = ",p)

#using while loop
    
n=(51,22,73,24,45)
p=0
i=0
while(i<len(n)):
    print(n[i])
    if(n[i]%2==0):
        p+=1
    i=i+1
print("Total even elements = ",p)

Output:

51
22
73
24
45
Total even elements =  2
>>> 

7.
Python program to traverse a tuple, calculate and display total even elements?
Sol:

#using for loop

n=(51,22,73,24,45)
p=0
for i in n:
    print(i)
    if(i%2==0):
        p+=1
print("Total even elements = ",p)

#using while loop
    
n=(51,22,73,24,45)
p=0
i=0
while(i<len(n)):
    print(n[i])
    if(n[i]%2==0):
        p+=1
    i=i+1
print("Total even elements = ",p)

Output:

51
22
73
24
45
Total even elements =  2
>>> 

8.
Python program to traverse a tuple, calculate and display total odd elements?
Sol:

#using for loop

n=(51,22,73,24,45)
p=0
for i in n:
    print(i)
    if(i%2==1): #if(i%2!=0):
        p+=1
print("Total odd elements = ",p)

#using while loop
    
n=(51,22,73,24,45)
p=0
i=0
while(i<len(n)):
    print(n[i])
    if(n[i]%2==1):  #if(i%2!=0):
        p+=1
    i=i+1
print("Total odd elements = ",p)

Output:

51
22
73
24
45
Total odd elements =  3
>>> 

9.
Python program to traverse a tuple, calculate and display the following:
* total +ve elements
* sum of all +ve elements
* average of all +ve elements
Sol:

#using for loop

n=(51,22,-73,24,-45)
p=0
s=0
av=0
for i in n:
    print(i)
    if(i>0):
        p+=1
        s=s+i
av=s/p
print("Total +ve elements = ",p)
print("Total +ve elements = ",s)
print("Average = ",av)

#using while loop
    
n=(51,22,-73,24,-45)
p=0
s=0
av=0
i=0
while(i<len(n)):
    print(n[i])
    if(n[i]>0):
        p+=1
        s=s+n[i]
    i=i+1
av=s/p
print("Total +ve elements = ",p)
print("Total +ve elements = ",s)
print("Average = ",av)

Output:

51
22
-73
24
-45
Total +ve elements =  3
Total even elements =  97
Average =  32.333333333333336
>>> 

10.
Python program to traverse a tuple, calculate and display the following:
* total even elements
* sum of all even elements
* average of all even elements
Sol:

#using for loop

n=(51,22,73,24,45)
p=0
s=0
av=0
for i in n:
    print(i)
    if(i%2==0):
        p+=1
        s=s+i
av=s/p
print("Total even elements = ",p)
print("Total even elements = ",s)
print("Average = ",av)
#using while loop
    
n=(51,22,73,24,45)
p=0
s=0
av=0
i=0
while(i<len(n)):
    print(n[i])
    if(n[i]%2==0):
        p+=1
        s=s+n[i]
    i=i+1
av=s/p
print("Total even elements = ",p)
print("Total even elements = ",s)
print("Average = ",av)

Output:

51
22
73
24
45
Total even elements =  2
Total even elements =  46
Average =  23.0
>>> 

Python Basic Programming Tutorial

Python Introduction     Getting started in Python Programming      Python propgramming fundamentals     Python Operators    Python If Condition     Python for loop    Python range construct      Python While loop    break and continue statements     Different looping techniques     Python List     Python String     Python Functions    Python Inbuilt Functions     Python Recursion     Using Python Library     Python Tuples     Python Dictionary     Python Sets     Python Strings     Python Exception Handling     Python Data File Handling

Visitor Counter

0060751