Objectives - PowerPoint PPT Presentation

1 / 19
About This Presentation
Title:

Objectives

Description:

Dictionaries in Python. Map keys to values. Keys are not necessarily alphabetized ... Why Dictionaries? Useful for fast lookups. Lookup by keyword ... – PowerPoint PPT presentation

Number of Views:61
Avg rating:3.0/5.0
Slides: 20
Provided by: sarasp8
Category:

less

Transcript and Presenter's Notes

Title: Objectives


1
Objectives
  • More on Dictionaries
  • Loose ends
  • Using/improving documentation
  • Default parameter values

2
Dictionaries 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?
3
Why Dictionaries?
  • Useful for fast lookups
  • Lookup by keyword
  • Dont need to look through entire list or sort
    list first

4
Examples 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

5
Creating Dictionaries in Python
  • Syntax
  • ltkeygtltvaluegt, , ltkeygtltvaluegt

empty ascii a97, b98, c99, ,
z122
6
Accessing Values using Keys
  • Syntax
  • ltdictionarygtltkeygt
  • Examples
  • KeyError if key is not in dictionary

asciiz directoryregistrar
7
Adding/Modifying Key-Value Pairs
  • Syntax
  • ltdictionarygtltkeygt ltvaluegt
  • asciia 97

ascii_dictionary.py
8
Dictionary Operations
Unlike strings and lists, doesnt make sense to
do slicing, concatenation, repetition for
dictionaries
9
Dictionary Methods
10
Special 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

11
Example Problem
  • Given a file of the form
  • ltlastnamegt ltmajorgt
  • Create a mapping between the last names and majors

majors_dictionary.py
12
Example 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
13
Getting 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)

14
Where 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
15
Defaults 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

16
Using 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
17
Problem 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)

18
Problem Student Majors, revised
  • Students can have more than one major
  • Should count these separately
  • How can we modify the previous program to do that?

19
Problem 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
Write a Comment
User Comments (0)
About PowerShow.com