Exercises

  1. 6.1 (Discussion: Dictionary Methods) Briefly explain the operation of each of the following dictionary methods:

    1. add

    2. keys

    3. values

    4. items

  2. 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}')
    
  3. 6.3 (What Does This Code Do?) The dictionary temperatures contains three Fahrenheit temperature samples for each of four days. What does the for 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.