Skip to content

Exercise 9: Dictionary

Alternative course

Dictionary

Python supports a data structure known as dictionary:  a set which contains key, value pairs. For every key in a dictionary,  there is a value. Unlike lists, dictionaries do not have an specific order.

Type the code below and run it:

#!/usr/bin/python

words = {}
words["BMP"] = "Bitmap"
words["BTW"] = "By The Way"
words["BRB"] = "Be Right Back"

print words["BMP"]
print words["BRB"]

You should see

python-dict

Study drill

Try the exercises below

1. Create a dictionary that maps these cities to the acronyms (NY,LA,AC,AM):
   list = [ "New York", "Los Angles", "Atlantic City", "Amsterdam" ]

After completing these continue with the next exercise.

Answers

Exercise 8: Lists
Exercise 10: Read file
Published inUncategorized

2 Comments

  1. Shaun Shaun

    I ran into an error with the code listed here. It flags me that I’m missing parenthesis in call to print. I fixed this by adding parenthesis such as print(words[“BMP”]). I don’t know if this is a typo or some other error, but it works with my solution.

Leave a Reply