this week dictionaries - not exactly webster’s using the debugger
TRANSCRIPT
![Page 1: This Week Dictionaries - not exactly Webster’s Using the debugger](https://reader035.vdocuments.us/reader035/viewer/2022072006/56649cfa5503460f949cc140/html5/thumbnails/1.jpg)
This Week
• Dictionaries
- not exactly Webster’s
• Using the debugger
![Page 2: This Week Dictionaries - not exactly Webster’s Using the debugger](https://reader035.vdocuments.us/reader035/viewer/2022072006/56649cfa5503460f949cc140/html5/thumbnails/2.jpg)
Dictionaries
• Generalizes the concept we are used to.• Have a key : value pair• Webster’s Dictionary
key = wordvalue = definition of word
• Use the key to locate the corresponding value
• We say the key indexes into the dictionary
• Keys are unique.
![Page 3: This Week Dictionaries - not exactly Webster’s Using the debugger](https://reader035.vdocuments.us/reader035/viewer/2022072006/56649cfa5503460f949cc140/html5/thumbnails/3.jpg)
Dictionary ExamplesStudent grades file
key = student number, value = grade
GDP for Canada since 1900key = year, value = GDP
Growth rate of fruit flieskey = time, value = number of fruit flies
![Page 4: This Week Dictionaries - not exactly Webster’s Using the debugger](https://reader035.vdocuments.us/reader035/viewer/2022072006/56649cfa5503460f949cc140/html5/thumbnails/4.jpg)
Dictionaries and Python
dict_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:emissions[1902] or
emissions.get(1902)
![Page 5: This Week Dictionaries - not exactly Webster’s Using the debugger](https://reader035.vdocuments.us/reader035/viewer/2022072006/56649cfa5503460f949cc140/html5/thumbnails/5.jpg)
Dictionaries and Python
dict_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}
Add entry:emissions[1950] = 734914
![Page 6: This Week Dictionaries - not exactly Webster’s Using the debugger](https://reader035.vdocuments.us/reader035/viewer/2022072006/56649cfa5503460f949cc140/html5/thumbnails/6.jpg)
Dictionaries and Pythondict_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}
Delete entry:del emissions[1902]
![Page 7: This Week Dictionaries - not exactly Webster’s Using the debugger](https://reader035.vdocuments.us/reader035/viewer/2022072006/56649cfa5503460f949cc140/html5/thumbnails/7.jpg)
Dictionaries and Pythondict_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}
Check for membership:1950 in emissions
![Page 8: This Week Dictionaries - not exactly Webster’s Using the debugger](https://reader035.vdocuments.us/reader035/viewer/2022072006/56649cfa5503460f949cc140/html5/thumbnails/8.jpg)
More Dictionary Operations
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)]
Looping over dictionaries:for key in emissions:
print(key + “, “ + emissions[key])
![Page 9: This Week Dictionaries - not exactly Webster’s Using the debugger](https://reader035.vdocuments.us/reader035/viewer/2022072006/56649cfa5503460f949cc140/html5/thumbnails/9.jpg)
More Dictionary Operations
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)]
Looping over dictionaries:for key in emissions.keys():
print(key + “, “ + emissions[key])
![Page 10: This Week Dictionaries - not exactly Webster’s Using the debugger](https://reader035.vdocuments.us/reader035/viewer/2022072006/56649cfa5503460f949cc140/html5/thumbnails/10.jpg)
More Dictionary Operations
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)]
Looping over dictionaries:for value in emissions.values():
print(value)
![Page 11: This Week Dictionaries - not exactly Webster’s Using the debugger](https://reader035.vdocuments.us/reader035/viewer/2022072006/56649cfa5503460f949cc140/html5/thumbnails/11.jpg)
More Dictionary Operations
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)]
Looping over dictionaries:for (key, value) in emissions.items():
print(key + ‘\t’ + value)
![Page 12: This Week Dictionaries - not exactly Webster’s Using the debugger](https://reader035.vdocuments.us/reader035/viewer/2022072006/56649cfa5503460f949cc140/html5/thumbnails/12.jpg)
More Dictionary Operationsemissions = {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}