Title: Objectives
1Objectives
- More on Dictionaries
- Loose ends
- Using/improving documentation
- Default parameter values
2Dictionaries in Python
- Map keys to values
- Keys are not necessarily alphabetized
- Mappings are from one key to one or more values
- Keys are unique, Values are not necessarily
unique - Example student ids --gt last names
- Keys must be immutable (numbers, strings)
- Similar to Hashtables/Hashmaps in other languages
How would we handle if there is more than one
value for a key?
3Why Dictionaries?
- Useful for fast lookups
- Lookup by keyword
- Dont need to look through entire list or sort
list first
4Examples of Dictionaries
- What are the keys and values for
- Dictionary
- Textbooks index
- Cookbook
- URL (Uniform Resource Locator)
- Examples from class
- Function name --gt function definition
- Variable name --gt value
- ASCII value --gt character
5Creating Dictionaries in Python
- Syntax
- ltkeygtltvaluegt, , ltkeygtltvaluegt
empty ascii a97, b98, c99, ,
z122
6Accessing Values using Keys
- Syntax
- ltdictionarygtltkeygt
- Examples
- KeyError if key is not in dictionary
asciiz directoryregistrar
7Adding/Modifying Key-Value Pairs
- Syntax
- ltdictionarygtltkeygt ltvaluegt
- asciia 97
ascii_dictionary.py
8Dictionary Operations
Unlike strings and lists, doesnt make sense to
do slicing, concatenation, repetition for
dictionaries
9Dictionary Methods
10Special Value None
- Special value we can use
- E.g., Return value from function when there is an
error - Similar to null in Java
- If you execute
- list list.sort()
- print list
- Prints None because list.sort() does not return
anything
11Example Problem
- Given a file of the form
- ltlastnamegt ltmajorgt
- Create a mapping between the last names and majors
majors_dictionary.py
12Example Problem
- Modify the previous program to keep track of the
number of majors of each type - Could we solve this using a list?
majors_dictionary2.py
13Getting Documentation
- dir function that returns a list of methods and
attributes in an object - dir(lttypegt)
- help get documentation
- In the Python shell
- help(lttypegt)
- import ltmodulenamegt
- help(ltmodulenamegt)
14Where is Documentation Coming From?
- Comes from the code itself in doc strings
- i.e., documentation strings
- Doc strings are simply strings after the function
header - Typically use triple-quoted strings because
documentation goes across several lines
def verse(animal, sound) prints a verse of
Old MacDonald, filling in the strings for animal
and sound
15Defaults for Parameters
- Saw with the xrange function
- Didnt have to specify start or increment when
calling the function - Default start to 0
- Default increment to 1
- Can assign a default value to a parameter
- In general, default parameter should come after
all the parameters that need to be defined
16Using Default Parameters
- By default the rollDie function could assume that
a die has 6 sides
Assigns a value ONLY IF not passed a parameter
def rollDie(sides6) return random.randint(1,si
des)
Examples of calling the function
rollDie(6) rollDie() rollDie(12)
game.py
17Problem Student Majors
- We want to keep track of the number of majors of
each type - Twist Not every student has a major (dont
declare until sophomore year)
18Problem Student Majors, revised
- Students can have more than one major
- Should count these separately
- How can we modify the previous program to do that?
19Problem Music Files
- We have an album file that has the format
- ltArtist namegt
- ltAlbum namegt
- ltSong name 1gt
- ltSong length 1gt
-
- ltSong name ngt
- ltSong length ngt
- Given an album file, print out the number of
songs and the total length of the album
Length has the format minseconds