Exercises
6.1 (Discussion: Dictionary Methods) Briefly explain the operation of each of the following dictionary methods:
add
keys
values
items
6.2 (What’s Wrong with This Code?) The following code should display the unique words in the string
text
and the number of occurrences of each word.from collections import Counter
text = ('to be or not to be that is the question')
counter = Counter(text.split())
for word, count in sorted(counter):
print(f'{word:<12}{count}')
6.3 (What Does This Code Do?) The dictionary
temperatures
contains three Fahrenheit temperature samples for each of four days. What does thefor
statement do?temperatures = {
'Monday': [66, 70, 74],
'Tuesday': [50, 56, 64],
'Wednesday': [75, 80, 83],
'Thursday': [67, 74 ...
Get Intro to Python for Computer Science and Data Science: Learning to Program with AI, Big Data and The Cloud now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.