Tuples are just like lists except that they are immutable (like strings) i.e. you cannot modify tuples. Tuples are defined by specifying items separated by commas within a 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 9.2. Using Tuples
#!/usr/bin/python # Filename : tuple.py zoo = ('wolf', 'elephant', 'penguin') print 'Number of animals in the zoo is', len(zoo) new_zoo = ('monkey', 'dolphin', zoo) print 'Number of animals in the new zoo is', len(new_zoo) print new_zoo # Prints all the animals in the new zoo print new_zoo[2] # Prints animals brought from the old zoo print new_zoo[2][2] # Prints the last animal brought from the old zoo
$ python tuple.py Number of animals in the zoo is 3 Number of animals in the new zoo is 3 ('monkey', 'dolphin', ('wolf', 'elephant', 'penguin')) ('wolf', 'elephant', 'penguin') penguin
Here, the variable zoo refers to a tuple of items. We see that the len function can be used to get the length of a tuple as well. 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 it's identity.
We can access the items in the tuple using the indexing operator just like we did for lists. We just specify the item's position within a pair of square brackets following the name of the tuple such as new_zoo[2]. In this case, this object is a tuple. So we can access items of this object as well using new_zoo[2][2].
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 single item so that Python can differentiate between a tuple and a pair of parentheses used for grouping in an expression i.e. you have to specify singleton = (some_item , ) .
A list within a list does not lose it's 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. As far as Python is concerned, they are just objects stored using another object, that's all.
Tuples are most often used alongwith the print statement. An example will make this clear.
Example 9.3. Output using tuples
#!/usr/bin/python # File : tuple.py age = 21 name = 'Swaroop' print '%s is %d years old' % (name, age) print "Why is %s playing with that python?" % name
The print statement takes a string using certain specifications followed by a % symbol which is followed by a tuple. The string can have specifications such as %s for strings and %d for integers. The tuple must have items corresponding to these specifications in the same order.
Observe the first usage where we have used %s first and this corresponds to the string name which is the first item in the tuple. Then, the second specification is %d which corresponds to the number age which is the second item in the tuple.
What Python does here is that it converts each item in the tuple into a string and substitutes that string value into the place of the specification. Therefore the %s in the first usage will be replaced by the value of the name variable, and so on.
This usage of the print statement makes writing output extremely easy and avoids using commas everywhere as we have done until now.
Most of the time, you can just use the %s specification and let Python take care of the rest for you. This works even for numbers, but you may want to give correct specifications in order to ensure that objects of proper type are being used.
In the second usage, we are using a single specification in the string followed by the % symbol followed by a single item - there are no pair of parentheses. This works only in the case where there is a single specification in the string.