Computer Science

What will be the output of the following code snippet?

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

Python Tuples

3 Likes

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.

Answered By

2 Likes


Related Questions