5.2.1 - Tuples

Tuples are very similar to lists, except that they are immutable (they cannot be changed). Also, they are created using parentheses, rather than square brackets.

>>> breakfast = ("spam", "eggs", "milk")

As with lists we can access the values in the tuple with their index:

>>> print (breakfast[0])

But we cannot reassign a value in a tuple. If we try python causes an error:

>>> breakfast[1] = "yougurt"

Tuples can be created without parentesis, by just separating the values with commas:

>>> my_tuple = "biscuits", "soda", "ice cream"

>>> print (my_tuple[0]) 

An empty tuple is created using an empty parentesis pair :

>>> tpl = () # But ..... what is the use of an empty tuple

If we can do the same things with lists why python has tuples? Tuples are faster than lists and use less memory, but they cannot be changed.