Tuples are a type of sequence, like strings. But unlike strings, which can only contain characters, tuples can contain elements of any type. That means you can have a tuple that stores a bunch of high scores for a game, or one that stores a group of employee names. But tuple elements don't have to all be of the same type. You could create a tuple with both strings and numbers, if you wanted. And you don't have to stop at strings and numbers. You can create a tuple that contains a sequence of graphic images, sound files, or even a group of aliens (once you learn how to create these things, which you will in later chapters). Whatever you can assign to a variable, you can group together and store as a sequence in a tuple.
Hero's Inventory maintains the inventory of a hero from a typical role-playing game. Like most role-playing games ever created, the hero is from a small, insignificant village. His father was, of course, killed by an evil warlord (What's a quest without a dead father?). And now that the hero has come of age, it's time for him to seek his revenge.
In this program, the hero's inventory is represented by a tuple. The tuple contains strings, one for each item in the hero's possession. The hero starts out with nothing, but then I give him a few items. Figure 4.11 shows the humble beginnings of our hero's journey.
Figure 4.11: At first, the hero has no items in his inventory. Then, the program creates a new tuple with string elements and our hero is stocked.
Here's the code for the program:
# Hero's Inventory # Demonstrates tuple creation # Michael Dawson - 1/29/03 # create an empty tuple inventory = () # treat the tuple as a condition if not inventory: print "You are empty-handed." raw_input("\nPress the enter key to continue.") # create a tuple with some items inventory = ("sword", "armor", "shield", "healing potion") # print the tuple print "\nThe tuple inventory is:\n", inventory # print each element in the tuple print "\nYour items:" for item in inventory: print item raw_input("\n\nPress the enter key to exit.")
To create a tuple, you just surround a list of values, separated by commas, with parentheses. Even a pair of lone parentheses is a valid (but empty) tuple. I created an empty tuple in the first part of the program to represent that the hero has nothing:
inventory = ()
It's as simple as that. So in this line, the variable inventory gets an empty tuple.
When you learned about conditions, you saw that you could treat any value in Python as a condition. That means you can treat a tuple as a condition, too. And that's what I did in the next lines:
if not inventory: print "You are empty-handed."
As a condition, an empty tuple is false. A tuple with at least one element is true. Since the tuple assigned to inventory is empty, it's false. That means not inventory is true. So the computer prints the string, "You are empty-handed.", just as it should.
An unarmed hero is a boring hero. So next, I created a new tuple with string elements that represent useful items for our hero. I assigned this new tuple to inventory with the following:
inventory = ("sword", "armor", "shield", "healing potion")
Each element in the tuple is separated by a comma. That makes the first element the string "sword", the next "armor", the next "shield", and the last element "healing potion". So each string is a single element in this tuple.
Also, notice that the tuple spans multiple lines. You can write a tuple in one line, or span it across multiple lines like I did, as long as you end each line after a comma. This is one of the few cases where Python lets you break up a statement across multiple lines.
TRICK | Make your programs easier to read by creating tuples across multiple lines. You don't have to write exactly one element per line, though. It might make sense to write several on a line. Just end each line at one of the commas separating elements and you'll be fine. |
Though a tuple can contain many elements, you can print the entire tuple just like you would any single value. That's what I did in the next line:
print "\nThe tuple inventory is:\n", inventory
The computer displays all of the elements, surrounded by parentheses.
Finally, I wrote a for loop to march through the elements in inventory and print each one individually:
print "\nYour items:" for item in inventory: print item
This loop prints each element (each string) in inventory on a separate line. This loop looks just like the ones you've seen with strings. In fact, you can use this kind of loop to go through the elements of any sequence.
Even though I created a tuple where all the elements are of the same type (strings in this case), tuples don't have to be filled with values of the same type. A single tuple can just as easily contain strings, integers, and floating-point numbers, for example.
TRAP | Other programming languages offer structures similar to tuples. Some go by the name "arrays" or "vectors." However, those other languages usually restrict the elements of these sequences to just one type. So, for example, you couldn't mix strings and numbers together. Just be aware that these other structures don't usually offer all the flexibility that Python sequences do. |