0% found this document useful (0 votes)
16 views

Cs 11 Python Hand Out 06

This document discusses Python dictionaries. It defines dictionaries as data structures that store key-value pairs, with keys being immutable types. It provides examples of defining and accessing dictionary elements, and lists common dictionary operations like getting values, adding/updating key-value pairs, checking for keys, and more. It also notes that dictionaries are unordered and cannot contain duplicate keys.

Uploaded by

Justin Tayaban
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Cs 11 Python Hand Out 06

This document discusses Python dictionaries. It defines dictionaries as data structures that store key-value pairs, with keys being immutable types. It provides examples of defining and accessing dictionary elements, and lists common dictionary operations like getting values, adding/updating key-value pairs, checking for keys, and more. It also notes that dictionaries are unordered and cannot contain duplicate keys.

Uploaded by

Justin Tayaban
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

CMSC11 Introduction to Computer Science

First Semester, A.Y. 2014-2015

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

Mutable (can be changed)

Immutable (cannot be changed)

Uses square brackets ( [ ] )

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

CMSC11 Introduction to Computer Science


First Semester, A.Y. 2014-2015

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)

Returns the number of key-value pairs in d.

d[k]

Returns the corresponding value v for the key k if k


exists; else, it raises a KeyError exception.

d[k] = v

Assigns the value v to the key k. If k does not exist in d,


a new pair k:v will be added.

k in d

Tests whether d has a key k.

k not in d

Tests whether d does not have a key k.

del d[k]

Deletes the key-value pair whose key equals k; if k


does not exist, it will raise an KeyError exception.

d.get(k)

Returns the value v of the key k. If k does not exist, it


returns None.

d.get(k,x)

Returns the value v of the key k. If k does not exist, it


returns x.

d.keys()

Returns a list of the key values in dictionary d.

d.popitem()

Returns an arbitrary entry from dictionary d as a (k,v)


tuple and removes that entry from the dictionary. If d
is empty, it returns a KeyError exception.

d.values()

Returns a list of the values from the key-value pairs in


dictionary d.

d.update(d1)

Merge the contents of dictionary d1 into dictionary d.

d.clear()

Removes all the key-value pairs in the dictionary

d.pop(k)

Returns the value v of key k and removes it from the


dictionary

d.copy()

Returns a copy of the dictionary

2 of 3

CMSC11 Introduction to Computer Science


First Semester, A.Y. 2014-2015

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

You might also like