This Week - PowerPoint PPT Presentation

1 / 6
About This Presentation
Title:

This Week

Description:

key = word. value = definition of word. Use the key to locate the corresponding value. Say the key indexes into the dictionary. Keys are unique. Dictionary Examples ... – PowerPoint PPT presentation

Number of Views:29
Avg rating:3.0/5.0
Slides: 7
Provided by: annabre
Category:

less

Transcript and Presenter's Notes

Title: This Week


1
This Week
  • while loops
  • Using the debugger
  • Dictionaries
  • not exactly Websters
  • Midterm Questions

in Wing
in PPT then wing
2
Dictionaries
  • Generalize the concept were used to.
  • Have a key value pair
  • Websters Dictionary
  • key word
  • value definition of word
  • Use the key to locate the corresponding value
  • Say the key indexes into the dictionary
  • Keys are unique.

3
Dictionary Examples
  • Student grades file
  • key student number, value grade
  • GDP for Canada since 1900
  • key year, value GDP
  • Growth rate of fruit flies
  • key time, value number of fruit flies

4
Dictionaries and Python
  • dictionary_name key1 value1, key2 value2,
  • Example
  • N. American fossil fuel emissions by year
  • Units are 1000s of metric tons of carbon
  • emissions 1799 1, 1800 70, 1801 74, 1802
    82,
  • 1902 215630, 2002 1733297
  • Look up entry
  • emissions1902 or emissions.get(1902)
  • Add entry
  • emissions1950 734914
  • Delete entry
  • del emissions1902
  • Check for membership
  • 1950 in emissions

5
More Dictionary Operations
  • Looping over dictionaries
  • for key in emissions
  • print key , emissionskey
  • Create a list of keys
  • emissions.keys() 1799, 1800, 1801, 1802,
    1902, 2002
  • Create a list of values
  • emissions.values() 1, 70, 74, 82, 215630,
    1733297
  • Create a list of (key, value) pairs
  • emissions.items() (1799, 1), (1800, 70),
    (1801, 74), (1802, 82), (1902, 215630), (2002,
    1733297)

6
More Dictionary Operations
  • emissions 1799 1, 1800 70, 1801 74, 1802
    82, 1902 215630, 2002 1733297
  • emissions2 1850 5418, 1875 37561, 1925
    486728, 1950 735036, 2004 1824421
  • Add the contents of one dictionary to another
  • emissions.update(emissions2)
  • emissions is
  • 1799 1, 1800 70, 1801 74, 1802 82,
    1902 215630, 2002 1733297, 1925 486728,
    1950 735036, 2004 1824421, 1850 5418, 1875
    37561
  • Clear the contents of a dictionary
  • emissions2.clear()
Write a Comment
User Comments (0)
About PowerShow.com