Friday, April 13, 2018

How to Accese Values in Tuples?


Accessing Values in Tuples

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

#!/usr/bin/python3

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]);

When the above code is executed, it produces the following result 
tup1[0]:  physics
tup2[1:5]:  [2, 3, 4, 5]