Lab 9: Dictionaries

This test will help you better understand how to use dictionaries.

Part 1: Using Dictionaries

In part 1 you’ll create and manipulate a dictionary.

1.1. Create a Dictionary

Create a dictionary called courses that contains the following:

Key

Value

cis-15

Cloud Programming in Python

cis-81

Introduction to Networking

cis-90

Introduction to UNIX/Linux

cis-75

Computer Security Fundamentals

[ ]:

1.2. Print a Key

Use the courses dictionary you created. Write a print statement that prints the title of cis-15.

[ ]:

1.3. Change a Value

The name of CIS-15 changed to “Python Programming for Everyone”. Change the value in courses to reflect the new name.

[ ]:

1.4. Add a Key

Write a statement that adds one key value pair:

  • Key: cis-54

  • Value: Introduction to Relational Databases

[ ]:

1.5. Remove a Key

Write a statement that removes cis-81 from the dictionary.

[ ]:

1.6. Test for a Key

Write an if statement that prints “Yes” if the cis-81 key is in courses and “No” otherwise.

[ ]:

1.7. More Key Tests

Use the input function to get a key from the user. Print “Yes” if the key exists in courses and “No” if it does not.

[ ]:

Part 2: The for Loop and Dictionaries

Now get practice using the for loops you learned.

2.1. Loop Over Keys

Write a for loop that prints all the keys in courses.

[ ]:

2.2. Loop Over Values

Write a for loop that prints all of the values in courses.

[ ]:

2.3. Keys and Values

Write a for loop that prints all keys and values in courses.

[ ]:

Part 3: Dictionary Functions

Here are some more challenging problems using dictionaries.

3.1. Reverse the Dictionary

Write a function caled reverse_get that takes two arguments, data and val. The function searches data for the value val and returns the corresponding key.

  • Name: reverse_get

  • Arguments:

    • data - A dictionary

    • val - A value in the dictionary

  • Returns: The key that matches the value or None if the value is not found in the dictionary.

[ ]:

3.2. Frequency Counter

Write a function that takes a single string argument and counts how many times each letter appears in the string. The function returns a dictionary with letters for keys and the count of the letters as values. For example if the function is given the sentence:

count_letters('Hello World')

It should return the dictionary:

{
    'h' : 1,
    'e' : 1,
    'l' : 3,
    ' ' : 1,
    'o' : 2,
    'w' : 1,
    'd' : 1,
}
  • Name: count_letters

  • Arguments:

    • text (string) - The text to count.

  • Returns: A dictionary with letters for keys and counts for values.

The nex cell has some example text to try:

[ ]:
example_text = """
It was a dark and stormy night; the rain fell in torrents — except
at occasional intervals, when it was checked by a violent gust of
wind which swept up the streets (for it is in London that our scene
lies), rattling along the housetops, and fiercely agitating the
scanty flame of the lamps that struggled against the darkness.
"""

Put your solution here:

[ ]:

[ ]: