FATHER AGNEL SCHOOL, NOIDA
GRADE XI - COMPUTER SCIENCE
PRACTICE WORKSHEET - 4 (TUPLES)
1. Which of the following will create a single element tuple ?
A. (1,)
B. (1)
C. ( [1] )
D. tuple([1])
2. What will be the output of the following Python code?
tp1 = (2,4,3)
tp3 = tp1*2
print(tp3)
A. (4,8,6)
B. (2,4,3,2,4,3)
C. (2,2,4,4,3,3)
D. Error
3. What will be the output of following Python code?
tp1 = (15,11,17,16,12)
tp1.pop(12)
print(tp1)
A. (15,11,16,12)
B. (15,11,17,16)
C. (15,11,17,16,12)
D. Error
4. Which of the following options will not result in an error when performed on types in Python where
tp = (5,2,7,0,3) ?
A. tp[1] = 2
B. tp.append(2)
C. tp1=tp+tp
D. tp.sum()
5. What will be the output of the following Python code ?
tp = ()
tp1 = tp * 2
print(len(tp1))
A. 0
B. 2
C. 1
D. Error
6. What will be the output of the following Python code ?
t = (10, 20, 30, 40, 50, 50, 70)
print(t[5:-1])
A. Blank output( )
B. (50,70)
C. (50,50,70)
D. (50,)
7. What is the difference between (30) and (30,) ?
8. Is the working of in operator and tuple.index( ) same ?
9. Find the output generated by following code fragment : (a, b, c, d) = (1, 2, 3)
10. Find the output generated by following code fragments :
tuple = ( 'a' , 'b', 'c' , 'd' , 'e')
tuple = ( 'A', ) + tuple[1: ]
print(tuple)
11. Find the output generated by following code fragments :
t3 = (6, 7)
t4 = t3 * 3
t5 = t3 * (3)
print(t4)
print(t5)
12. What does each of the following expressions evaluate to? Suppose that T is the tuple containing :
("These", ["are" , "a", "few", "words"] , "that", "we", "will" , "use")
A. T[1][0: :2]
B. "a" in T[1][0]
C. T[:1] + [1]
D. T[2::2]
E. T[2][2] in T[1]
13. What would be the output of following code if
ntpl = ("Hello", "Nita", "How's", "life?")
(a, b, c, d) = ntpl
print ("a is:", a)
print ("b is:", b)
print ("c is:", c)
print ("d is:", d)
ntpl = (a, b, c, d)
print(ntpl[0][0]+ntpl[1][1], ntpl[1])
14. Predict the output.
tuple_a = 'a', 'b'
tuple_b = ('a', 'b')
print (tuple_a == tuple_b)
Practice codes
15. Write a program that interactively creates a nested tuple to store the marks in three subjects for five
students, i.e., tuple will look somewhat like :
marks( (45, 45, 40), (35, 40, 38), (36, 30, 38), (25, 27, 20), (10, 15, 20) )
num_of_students = 5
tup = ()
for i in range(num_of_students):
print("Enter the marks of student", i + 1)
m1 = int(input("Enter marks in first subject: "))
m2 = int(input("Enter marks in second subject: "))
m3 = int(input("Enter marks in third subject: "))
tup = tup + ((m1, m2, m3),)
print()
print("Nested tuple of student data is:", tup)
16. Write a program that calculates and displays the mean of a tuple with numeric elements.
tup = eval(input ("Enter the numeric tuple: "))
total = sum(tup)
tup_length = len(tup)
mean = total / tup_length
print("Mean of tuple:", mean)
17. Create a tuple containing the squares of the integers 1 through 50 using a for loop.
tup = ()
for i in range(1,51):
tup = tup + (i**2,)
print("The square of integers from 1 to 50 is:" ,tup)
***********