Lecture 43: + HW

posted Apr 21, 2013, 9:09 PM by Samuel Konstantinovich   [ updated Apr 21, 2013, 9:26 PM ]
You should take work I give you when I am absent seriously. Please read very carefully, and be sure to print your homework (with header) tomorrow.

I. What is a python Dictionary? 

Dictionaries are containers like lists, but they store pairs of information called items. An item is a pair that contains a key and a corresponding valueDictionaries are not ordered. There is no 1st or 2nd item.

II. Example of the syntax to create a dictionary is as follows:

    dict = {'Alice': '2341', 'Beth': '9102', 'Cecil': '3258'}

'Alice':'2341' is the first item of this dictionary. 
'Alice' is the key, 
'2341' is the value that corresponds to the key 'Alice'.

Each key is separated from its value by a colon (:), the items are separated by commas, and the whole thing is enclosed in curly braces. An empty dictionary without any items is written with just two curly braces, like this: {}.

Think of keys as a really awesome index for a super flexible list. Since they act like an index Keys must be unique within a dictionary while values may not be. The values of a dictionary can be of any type, but the keys must be of an immutable data type such as strings or numbers.



III. Example of syntax to get values from a dictionary or put them there:
we can access the values of a dictionary by placing the key into square braces much like the index of a list:

e = {'I':1 , 'II':2 , 'III':3, 'IIV':3 , 'IV':4, 'V':5, 'VI':6}
#now we try e[key]  will get the value associated with the key provided
print e['I']  -> prints 1
print e['IV'] -> prints 4
print e[2] -> causes an error, there is no key 2.

#we can assign keys values
e['X'] = 10

#or re-assign them
e['I'] = 0.999

As you can see there are some similarities to lists. 

Remember keys must be unique but values can repeat:
d = {"cat": 3, "dog":5, "cow":3, "frog": 3, "moose":1, "bird":5}
print d  
# prints:  {'moose': 1, 'cow': 3, 'dog': 5, 'frog': 3, 'cat': 3, 'bird': 5}

#As you can see that 3 can appear more than once

#make some changes to the animal list (d)
d['fish'] = 3 
d['cow'] = 4
#cow cannot appear more than once, so this changes the value paired with cow to 4

print d 
#this would print: {'moose': 1, 'cow': 4, 'fish': 3, 'dog': 5, 'frog': 3, 'cat': 3, 'bird': 5}


IV. Methods of Dictionaries: (Assume D is a dictionary)

The len function works on dictionaries.  len(D) evaluates to the number of items in D.

D.items() - returns a list of all the items in D

D.keys() - returns a list of all the keys of D

D.has_key( key ) - returns True if the key exists False otherwise. Useful to use before you try to get a value. 

D.pop(key) - works as you would expect, removes the key:value from the dictionary, and returns the value. If the key is not found, an error occurs.

but it has an extra feature:
D.pop(key,default_value) - Same as above except: If the key is not found, return the default_value. This is like the  -1 with find in a string, except you specify the special value yourself. 

You can read a more complete list of methods here: http://docs.python.org/2/library/stdtypes.html#mapping-types-dict

V. Your classwork:
1. Try the different functions and make sure you can use them. 

   (2 and 3 can be created manually, but you should print them and test them to check if it worked.)
2Make a dictionary D1 consisting of 4 keys, the keys should be strings. The values should be a number. 
3. Make a dictionary D2 consisting of 5 keys, the keys should be numbers, the values should be the word of the number like   5:'five' 

4. Make python code that prints out a list of the keys of D2.   e.g. [5,3,9,12,44]
5. Make python code that prints out each of the keys of D2 one at a time (don't print a whole list)
6. Make python code that prints out each VALUE of D2 one at a time.  
7. Make python code that prints out a list of all VALUES of D2. e.g. ['five','three','nine','twelve','forty four']

VI. Homework: (Print it and bring to class tomorrow)
1. Can dictionaries be reversed so that the keys and values are swapped:  { key1:value1 , key2:value2 ... }  becomes {value1:key1 , value2:key2 ...}. Why or why not? 

2. What are 3 examples of things you would use a dictionary to store in a python program. For each example a) What would the key/value pairs be? b) Why is a dictionary better than a list?  
e.g. (You can't use this)
An obvious use for dictionaries in Python is that one can be used to store a _Dictionary_ (the book with words)
a)Keys would be the words, and values would be the definitions. 
b)...

3. Make functions to do the following:
makeSquareDict(a,b) a and b are integers such that a<=b. The function will return a dictionary with keys starting at a , ending at but not including b. The values of the items are the squares of the keys.  so:
e.g.
makeDict(3,7) -> { 3:9, 4:16, 5:25, 6:36}

4. Test your function with 5 different examples. 
Make sure you include negative numbers, as well as a case when a=b.

Comments