Skip to main content

8.4) Tuples


A tuple is not a strange animal from a far away continent, it's a data structure much like a list, except it can't be modified: it's immutable, like a string.

You create a Tuple, like a list, but using round brackets instead of square brackets.

>>> planets=('Mercury','Venus','Earth','Mars', 'Jupiter','Saturn','Uranus','Neptune')

Just like for lists, you can select a specific element (using square brackets) and concatenate two tuples:

>>> planets[0]
'Mercury'
>>> (1,'two',3)+(4,5,6)
(1, 'two', 3.0, 4, 5, 6)

Tuples can't be modified. So the following won't work:

>>> planets[2]='Moon'   # This will return an error

You can easily create a list from a Tuple and vice versa.

>>> my_list=list(planets)
>>> my_tuple=tuple(my_list)