Python Data Types

Python Data Type – Tuple

Python supports another data type called Tuple.  The Tuple data type is also a list.  But it is different from the list data type in that whereas you can change the items in the list, you cannot change the items in a tuple. In other words, tuples are immutable (not allowed to be changed).

Similar to a list, a tuple consists of several items separated by commas. Whereas items in a list are enclosed in square brackets […], the tuple data type items are enclosed in parenthesis (…). When Python sees parenthesis instead of square brackets, it assumes that the list is a tuple.

Why Use Tuple Data Types

There are cases where you want to make sure that the items in the list are not able to be changed. Suppose you are using a Python program to make a list of students, who are on the football team. You want to make sure that some instruction in your program does not inadvertently change the names, or add/delete the name of the team member. In a case like this, where you want to make sure that the items in the list do not get changed, you will use a tuple data type instead of a list data type.  You will see extensive use of tuple data type in the Pygame chapter.

Tuple Concatenation

You learned about the concatenation (addition) of lists in the previous section. Concatenation of tuples works the same way as in lists. The plus (+) symbol is used for the concatenation of two or more tuples.

Below we define two tuples: earth_tuple and mars_tuple. Then we use the plus (+) symbol to concatenate (add) them. Remember, both the list data type and the tuple data type can consist of numbers, characters, as well as strings.

>>> earth_tuple = (‘earth has one moon ’, 250000 , ‘miles from earth’)
>>> mars_tuple = (‘Mars has two moons called’, ‘Deimos’, ‘ and Phobos’) 
PROGRAM EXAMPLE: TUPLE CONCATENATION
>>> # Tuple concatenation
>>> mars_tuple = ('Mars has two moons called ', 'Deimos ', 'and Phobos.')
>>> earth_tuple = ('earth has one moon ', 250000 , 'miles from earth. ')
>>> # Now we concatenate earth_tuple and mars_tuple.
>>> earth_tuple + mars_tuple
('earth has one moon ', 250000, 'miles from earth. ', 'Mars has two moons called ', 'Deimos ', 'and Phobos.')
>>>

In the program above, the concatenation symbol joins the items in the tuples “earth_tuple” and “mars_tuple”.

Tuple Multiplication

If you want to repeat the items in a tuple a few times, the procedure to repeat the items is the same as for the list data type. Tuples can be multiplied by using the multiply (*) symbol.  The items in the tuple will be repeated the number of times as specified by the multiplier operand.

In the program below, we define tuple1 = (‘earth’, ‘moon’). Then, we multiply tuple1 by 3.

PROGRAM EXAMPLE: TUPLE MULTIPLICATION
>>> tuple1 = ('earth', ' moon')
>>> tuple1 * 3
('earth', ' moon', 'earth', ' moon', 'earth', ' moon')
>>>

Tuple Indexing

Index in tuples follows the same rules as for list data type. The first item in the tuple has an index = 0; the next item has an index = 1, and so on. Indexing is used to select one or a few contiguous items from a long list of items.  Continuing with our solar system example, we define a tuple, called planets as below.

>>> planets = (‘mercury’,’venus’,’earth’,’mars’,’jupiter’,’saturn’, ’uranus’,’neptune’)

The index for each planet is in the table below.

Namemercuryvenusearthmarsjupitersaturnuranusneptune
Index01234567
Indexing In Tuples

If we want to print planet Jupiter, we will use index = 4.  We can print several contiguous items by using slicing.

The program example below specifies the index [2:6] to print all planets from index 2 through index 5.

PROGRAM EXAMPLE: TUPLE INDEXING
>>> # Index example in Tuples data type
>>> # Define tuple called 'planet'.
>>> planet = ('mercury','venus','earth','mars','jupiter','saturn','uranus','neptune')
>>> print(planet[4])
jupiter
>>> print(planet[7])
neptune
>>> print(planet[2:6])
('earth', 'mars', 'jupiter', 'saturn')
>>> 

Test if an Item in a Tuple List Can Be Changed

We mentioned earlier that the tuple data type is immutable, that is the item values in a tuple cannot be changed. Let us test if that is indeed true. The program below defines a tuple named “tupleA”. The statement

tupleA[0] = ‘Mars’

attempts to replace the item “earth” with “Mars”.

>>> tupleA = (‘earth’, ‘moon’, 250000, ‘miles’)

PROGRAM EXAMPLE: TEST THAT TUPLE DATA TYPE IS IMMUTABLE
>>> tupleA = ('earth', 'moon', 250000, 'miles')
>>> tupleA[0] = 'Mars' 
# Try to change ‘earth’ to ‘Mars’ to check if tuple type is immutable.
Traceback (most recent call last):
  File "<pyshell#22>", line 1, in <module>
    tupleA[0] = 'Mars'
TypeError: 'tuple' object does not support item assignment

Note that Python gives a syntax error and does not allow to change any item in a tuple.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Verified by MonsterInsights