Accessing Values of Tuples in Python



To access values in tuple, use the square brackets for slicing along with the index or indices to obtain value available at that index.

Example

 Live Demo

#!/usr/bin/python
tup1 = ('physics', 'chemistry', 1997, 2000);
tup2 = (1, 2, 3, 4, 5, 6, 7 );
print "tup1[0]: ", tup1[0];
print "tup2[1:5]: ", tup2[1:5];

Output

When the above code is executed, it produces the following result −

tup1[0]: physics
tup2[1:5]: [2, 3, 4, 5]

Advertisements