How to access element in a Python tuple with index

Access elements in tuple with index

All elements in a tuple is numbered and we can access element by the numbers. The number starts from zero.


x = (1, 2, 3)
print x[1] 

The code above generates the following result.

From the code above we can see that the elements in accessed by the index.

If the number is negative Python counts the index from right of the list. -1 is the right most element.

                                        
t = ("a", "b", "c", "z", "example")
print t 
print t[0]                                           
print t[-1]  

The code above generates the following result.





















Home »
  Python »
    Data Types »




Data Types
String
String Format
Tuple
List
Set
Dictionary