Certified Python Developer Learning Resources Tuple

Learning Resources
 

Tuple


Tuples are used to hold together multiple objects. Think of them as similar to lists, but without the extensive functionality that the list class gives you. One major feature of tuples is that they are immutable like strings i.e. you cannot modify tuples.

Tuples are defined by specifying items separated by commas within an optional pair of parentheses.

Tuples are usually used in cases where a statement or a user-defined function can safely assume that the collection of values i.e. the tuple of values used will not change.

Example:

#!/usr/bin/python
# Filename: using_tuple.py
 
zoo = ('python', 'elephant', 'penguin') # remember the parentheses are optional
print('Number of animals in the zoo is', len(zoo))
 
new_zoo = 'monkey', 'camel', zoo
print('Number of cages in the new zoo is', len(new_zoo))
print('All animals in new zoo are', new_zoo)
print('Animals brought from old zoo are', new_zoo[2])
print('Last animal brought from old zoo is', new_zoo[2][2])
print('Number of animals in the new zoo is', len(new_zoo)-1+len(new_zoo[2]))

Output:

   $ python using_tuple.py
   Number of animals in the zoo is 3
   Number of cages in the new zoo is 3
   All animals in new zoo are ('monkey', 'camel', ('python', 'elephant', 'penguin'))
   Animals brought from old zoo are ('python', 'elephant', 'penguin')
   Last animal brought from old zoo is penguin
   Number of animals in the new zoo is 5

How It Works:

The variable zoo refers to a tuple of items. We see that the len function can be used to get the length of the tuple. This also indicates that a tuple is a sequence as well.

We are now shifting these animals to a new zoo since the old zoo is being closed. Therefore, the new_zoo tuple contains some animals which are already there along with the animals brought over from the old zoo. Back to reality, note that a tuple within a tuple does not lose its identity.

We can access the items in the tuple by specifying the item's position within a pair of square brackets just like we did for lists. This is called the indexing operator. We access the third item in new_zoo by specifying new_zoo[2] and we access the third item within the third item in the new_zoo tuple by specifying new_zoo[2][2]. This is pretty simple once you've understood the idiom.

Parentheses - Although the parentheses are optional, I prefer always having them to make it obvious that it is a tuple, especially because it avoids ambiguity. For example, print(1,2,3) and print( (1,2,3) ) mean two different things - the former prints three numbers whereas the latter prints a tuple (which contains three numbers).
Tuple with 0 or 1 items - An empty tuple is constructed by an empty pair of parentheses such as myempty = (). However, a tuple with a single item is not so simple. You have to specify it using a comma following the first (and only) item so that Python can differentiate between a tuple and a pair of parentheses surrounding the object in an expression i.e. you have to specify singleton = (2 , ) if you mean you want a tuple containing the item 2.
Note for Perl programmers - A list within a list does not lose its identity i.e. lists are not flattened as in Perl. The same applies to a tuple within a
tuple, or a tuple within a list, or a list within a tuple, etc. As far as Python is concerned, they are just objects stored using another object, that's all.
 
-Swaroopch
 
 For Support