CBSE Class 11 Computer Science Question 97 of 114

Tuples — Question 34

Back to all questions
34
Question

Question 10

What will be the output of the following code snippet?

Tup1 = ((1, 2),) * 7
print(len(Tup1[3:8]))
Answer
Output
4
Explanation

* operator repeats ((1, 2),) seven times and the resulting tuple is stored in Tup1. Therefore, Tup1 will contain ((1, 2), (1, 2), (1, 2), (1, 2), (1, 2), (1, 2), (1, 2)).

Tup1[3:8] will create a tuple slice of elements from index 3 to index 7 (excluding element at index 8) but Tup1 has total 7 elements, so it will return tuple slice of elements from index 3 to last element i.e ((1, 2), (1, 2), (1, 2), (1, 2)).
len(Tup1[3:8]) len function is used to return the total number of elements of tuple i.e., 4.