What is a tuple?
Tuples vs. lists: similarities & differences.
What is a Python dictionary?
Why are dictionaries useful?
A tuple is very similar to a list. Like a list, a tuple holds a collection of values. Let's create some tuples:
coordinates = (5,2)
desserts = ('cake', 'icecream', 'pie', 'candy', 'brownies')
birthday = ('March', 12, 2001)
Look closely at the code. How does it differ from how we create lists?
Just like a list, we can access a value from a tuple using an index:
print 'x:', coordinates[0]
print 'y:', coordinates[1]
We can get the length of a tuple using len(), and we can use a for loop to access every value in a tuple (also like lists!):
print 'Number of desserts:', len(desserts)
print 'The desserts are'
for tastyFood in desserts:
print tastyFood
So far, tuples look very similar to lists. However, a tuple has one very important difference from a list: after a tuple is created, the values inside the tuple cannot be changed! This code will not run:
fibo = (1,1,2,3,5,6,13)
fibo[5] = 8
If tuples are so similar to lists, then what's the big deal? Well, since the values in tuples can't be modified, a tuple is really useful for keeping track of information that shouldn't change - like your friend's birthdate! And here's something useful you can do with tuples that you can't do with lists:
name = ('Dr.', 'Shrideep', 'Pallickara')
(title, firstName, lastName) = name
print firstName
print lastName
Notice how we've extracted the values from the tuple into three different variables. We can now use each of the variables on its own when we need a particular value! Tuples are also helpful if you want to be able to return multiple values from a function:
def divide(x, y):
quotient = x / y
remainder = x % y
return (quotient, remainder)
(q, r) = divide(25, 4)
A dictionary in Python is similar to a list, except that instead of using numbers for indexes, we can choose whatever labels we like for the values in the dictionary. (These labels are called keys.) For example, suppose you have a bag of m&ms and you've counted how many of each color there are:
red: 10
green: 13
blue: 4
brown: 7
You could store the information in a list:
candyList = [10,13,4,7]
This would make a list which looks like this:
Index | 0 | 1 | 2 | 3 |
---|---|---|---|---|
Value | 10 | 13 | 4 | 7 |
Do you see a problem here?
It's no longer clear which value belongs with which color. You'd have to remember the order you put them into the list, and that's not very convenient. Instead, let's use a dictionary!
candyDict = {'red': 10, 'green': 13, 'blue': 4, 'brown': 7}
This will make a dictionary which looks like this:
Key | 'red' | 'green' | 'blue' | 'brown' |
---|---|---|---|---|
Value | 10 | 13 | 4 | 7 |
Now, when you want to get a value from the dictionary, you use its key:
print 'Red m&ms:', candyDict['red']
print 'Blue m&ms:', candyDict['blue']
Suppose you suddenly discover a special purple m&m. You can add that to your dictionary:
candyDict['purple'] = 1
And if you eat 2 of your blue m&ms, you need to update the value for 'blue' in your dictionary.
candyDict['blue'] = candyDict['blue'] - 2
Here's a dictionary that might come in handy for you.
school = {}
school['name'] = 'Cortez Middle School'
school['street address'] = '450 West 2nd St'
school['city'] = ('Cortez', 'Colorado')
school['zipcode'] = 81321
print school
Notice that we created an empty dictionary at first using {}, and that values in a dictionary can be words or tuples as well as numbers.