CS - XI - Worksheet-4 (Tuples)
CS - XI - Worksheet-4 (Tuples)
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()
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]
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()
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)
***********