Cs 11 Python Hand Out 06
Cs 11 Python Hand Out 06
Handout 6
Dictionaries
Last week, we discussed about some of Python's built-in data structures, strings and lists. For this week, we will
be discussing another two data structure in Python: tuples and dictionaries.
TUPLES
Tuples are also ordered sequences of elements, just like lists. The only difference is that tuples can't be
changed.
LISTS
VS
TUPLES
Uses parentheses ( ( ) )
Defining a tuple:
Creating a tuple is just the same in creating a list, however, instead of square braces, parentheses are used.
tuple1 = (I, am, a, tuple)
tuple2 = (1,2,one,two)
Operations:
The same operations that can be used on lists can also be used on tuples. However, since tuples are immutable,
an assignment statement will not work. Thus, a statement like
tuple1[1] = am not
will give an error.
NOTE:
Remember that lists can contain any type of object? Tuples can also contain any type! That means the following
statement is valid:
tup = ([1,2,3], pie, 3.14, 777)
Since tuples are immutable, then the statement
tup[0] = [3,1,4]
will give an error. How about this statement:
tup[0][0] = 2
Will it give an error?
DICTIONARIES
Unlike sequences, i.e. tuples, lists, strings, which are indexed by a range of numbers, dictionaries are indexed by
keys, which can be any immutable type. Dictionaries define one-to-one relationships between keys and values.
Defining a dictionary
To define a dictionary, the following format must be followed:
{ [key1:value1, key2:value2, ...] }
where key1, key2, , keyn are immutable type objects, and
value1, value2, , valuen are the values of the respective keys.
NOTE: Dictionaries are enclosed by curly braces ( {} ). Also, the key-value pairs are inside the square braces
since they are optional, that means that a declaration like a = {} is valid.
Also, the previous data structures discussed all have ordered elements. Dictionaries, on the other hand, have
unordered elements.
1 of 3
Handout 6
Dictionaries
Try these!
charmander = {level:5,type:Fire,HP:25}
print(charmander[level])
print(charmander)
#what will this print?
charmander['evo'] = Charmeleon
print(charmander)
#how about this?
NOTE:
You cannot have duplicate keys in a dictionary. Assigning a value to an existing key will wipe out the old value.
Also, you can add any new key-value pair at anytime just like how we add the key evo and the value
Charmeleon to the dictionary charmander.
Operations on dictionaries:
Operation/Method
Description
len(d)
d[k]
d[k] = v
k in d
k not in d
del d[k]
d.get(k)
d.get(k,x)
d.keys()
d.popitem()
d.values()
d.update(d1)
d.clear()
d.pop(k)
d.copy()
2 of 3
Handout 6
Dictionaries
TRY THESE!
gymLeaders = {pewter:brock, azalea:bugsy, fuchsia:koga}
gymLeaders2 = gymLeaders
print(gymLeaders)
print(gymLeaders2)
gymLeaders.popitem()
print(gymLeaders)
print(gymLeaders2)
What happened?
When you assign a list/dictionary to another variable, it does not create a new set of data. Both variable would
be pointing to only one data. That means there are two variables pertaining to one data. To create another
variable with the same set of data, make sure to use the method copy().
3 of 3