Tuple Practice Questions

Question:1

GIve out put of the following program?

n=('c','o','m','p','u','t','e','r')
 print(n)
 print(n[4])
 print(n[3])
 print(n[6])
 print(n[2])
 print(n[1])
 print(n[7])

Output:

(‘c’, ‘o’, ‘m’, ‘p’, ‘u’, ‘t’, ‘e’, ‘r’)
u
p
e
m
o
r
>>>

Question:2

Give output of the following python script?

n=('c','a','t','a','l','y','s','t')    
 print(n)
 print(n[1])
 print(n[6])
 print(n[2])
 print(n[7])
 print(n[3])
 print(n[2.4]) 

Output:

(‘c’, ‘a’, ‘t’, ‘a’, ‘l’, ‘y’, ‘s’, ‘t’)
a
s
t
t
a

Error

Question:

Write a python script to display the elements of a tuple using a for loop?

n=(1,2,3,4,5,6,7,8,9,10)
 print(n)
 for i in n:
     print(n[i-1])

Output:

(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
1
2
3
4
5
6
7
8
9
10
>>>

Question:

Write a python script to display all elements of given tuple:

n=(23,45,65,67,87,56,34)

n=(23,45,65,67,87,56,34)
 print(n)
 l=len(n)
 print("len = ",l)
 n1=list(range(0,l))
 print(n1)
 for i in n1:
     print(n[i])

Question:

Given a tuple
n=(2,34,5,6,78,9,56,4)
* display elements of tuple
* display sum of all the tuple element

n=(2,34,5,6,78,9,56,4)
 print(n)
 l=len(n)
 #print("len = ",l)
 n1=list(range(0,l))
 s=0
 for i in n1:
     print(n[i])
     s=s+n[i]
 print("Sum of all the elements ",s)

Question:

Given a tuple
n=(-9,34,-25,6,8,9,-6,34)
* print total +ve elements in tuple

n=(-9,34,-25,6,8,9,-6,34)
 l=len(n)
 print("len = ",l)
 n1=list(range(0,l))
 c=0
 for i in n1:
     print(n[i])
     if(n[i]>0):
         c=c+1
 print("Total +ve elements ",c)

Question:

Given a tuple
n=(-9,34,-25,6,8,9,-6,34)
* print total -ve elements in tuple

n=(-9,34,-25,6,8,9,-6,34)
 l=len(n)
 print("len = ",l)
 n1=list(range(0,l))
 c=0
 for i in n1:
     print(n[i])
     if(n[i]<0):
         c=c+1
 print("Total -ve elements ",c)

Question:

Given a tuple
n=(9,34,25,6,8,7,6,37)
* print total even elements in tuple

n=(9,34,25,6,8,7,6,37)
 l=len(n)
 print("len = ",l)
 n1=list(range(0,l))
 c=0
 for i in n1:
     print(n[i])
     if(n[i]%2==0):
         c=c+1
 print("Total even elements ",c)

Question:

Given a tuple
n=(19,34,55,33,6,8,17,6,37)
* print total odd elements in tuple

n=(19,34,55,33,6,8,17,6,37)
 l=len(n)
 print("len = ",l)
 n1=list(range(0,l))
 c=0
 for i in n1:
     print(n[i])
     if(n[i]%2==1):
         c=c+1
 print("Total odd elements ",c)

Question:

Given a tuple
n=(2,34,5,6,78,9,56,4)
* display elements of tuple
* display largest tuple elements

n=(2,34,5,6,78,9,56,4)
 l=len(n)
 print("len = ",l)
 n1=list(range(0,l))
 print(n1)
 m=0
 for i in n1:
     if(i==0):
         m=n[i]
     if(n[i]>m):
         m=n[i];
     print(n[i])
 print("max element = ",m)

Questions:

Given a tuple
n=(20,3,15,6,178,2,56,4)
* display elements of tuple
* display lowest tuple element

n=(20,3,15,6,178,2,56,4)
 l=len(n)
 print("len = ",l)
 n1=list(range(0,l))
 print(n1)
 m=0
 for i in n1:
     if(i==0):
         m=n[i]
     if(n[i]<m):
         m=n[i];
     print(n[i])
 print("min element = ",m)

Questions:

Given a tuple
n=(29,134,25,86,378,239,6,34)
* display elements of tuple
* display lowest tuple elements
* display largest tuple elements
* difference between largest and lowest tuple element

n=(29,134,25,86,378,239,6,34)
 l=len(n)
 print("len = ",l)
 n1=list(range(0,l))
 print(n1)
 ma=0
 mi=0
 for i in n1:
     if(i==0):
         ma=n[i]
         mi=n[i]
     #max no
     if(n[i]>ma):
         ma=n[i];
     #min no
     if(n[i]<mi):
         mi=n[i]
     print(n[i])
 print("max element = ",ma)
 print("min element = ",mi)
 d=ma-mi
 print("Differnece between max and min element  = ",d)

Number Of Visitors

0238233