Wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

Ch. 9 Intro To Python: Lesson 9-6 Dictionaries

Total questions: 10

Worksheet time: 20mins

Name
Class
Date
1.

Which of the following are true of Python dictionaries:

select all that apply

a)

All the keys in a dictionary must be of the same type.

b)

Dictionaries can be nested to any depth.

c)

Dictionaries are accessed by key.

d)

A dictionary can contain any object type except another dictionary.

e)

Dictionaries are mutable.

2.

Which of the following is not a valid way to define this dictionary in Python:

a)

d = dict([

('foo', 100),

('bar', 200),

('baz', 300) ])

b)

d = {'foo': 100, 'bar': 200, 'baz': 300}

c)

d = { ('foo', 100), ('bar', 200), ('baz', 300) }

d)

d = {} d['foo'] = 100 d['bar'] = 200 d['baz'] = 300

e)

d = dict(foo=100, bar=200, baz=300)

3.

What statement will remove the entry in the dictionary for key 'baz'?

(a)  

4.

Write the Python code that prints out Captain Kirk’s email address from the employee dictionary below:

Add your code below:

(a)  

5.

Which Python code could replace the ellipsis (...) below to get the following output to the left?

(Select all that apply.)

a)

for ship, captain in captains.items(): print(ship, captain)

b)

for ship in captains: print(ship, captains)

c)

for ship in captains: print(ship, captains[ship])

6.

Which of the following statements about this nested dictionary are true? (Select all that apply.)

a)

The values for "California", "New York", and "Texas" are all dictionaries.

b)

You can access the nested values directly by chaining square brackets ([]).

c)

If you want to access the nested values, then you need to split the dictionary.

d)

The syntax isn’t valid.

7.

Which of the following lines of code will create an empty dictionary named captains?

a)

captains.dict()

b)

captains = {dict}

c)

type(captains)

d)

captains = {}

8.

Now you have your empty dictionary named captains. It’s time to add some data!

Specifically, you want to add the key-value pairs "Enterprise": "Picard", "Voyager": "Janeway", and "Defiant": "Sisko".

Which of the following code snippets will successfully add these key-value pairs to the existing captains dictionary?

a)

captains{"Enterprise" = "Picard"} captains{"Voyager" = "Janeway"} captains{"Defiant" = "Sisko"}

b)

captains = { "Enterprise": "Picard", "Voyager": "Janeway", "Defiant": "Sisko", }

c)

captains["Enterprise"]: "Picard" captains["Voyager"]: "Janeway" captains["Defiant": "Sisko"

d)

captains["Enterprise"] = "Picard" captains["Voyager"] = "Janeway" captains["Defiant"] = "Sisko"

9.

Yay! Your dictionary is now full of data:

captains = { "Enterprise": "Picard", "Voyager": "Janeway", "Defiant": "Sisko", }

At this point, you might want to check whether some specific keys exist in the dictionary. If they don’t, then you’ll want to add them. Which of the following code snippets accomplishes this task?

a)

if "Enterprise" not in captains: captains["Enterprise"] = unknown

if "Discovery" not in captains: captains["Discovery"] = unknown

b)

for "Enterprise" not in captains: captains["Enterprise"] = "unknown"

for "Discovery" not in captains: captains["Discovery"] = "unknown"

c)

if "Enterprise" not in captains: captains["Enterprise"] = "unknown"

if "Discovery" not in captains: captains["Discovery"] = "unknown"

10.

You’re really building out the Federation Starfleet now! Here’s what you have:

Now, say you want to display the ship and captain names contained in the dictionary, but you also want to provide some additional context. How could you do it?

a)

for captain, ship in captains.items(): print(f"The {ship} is captained by {captain}.")

b)

for ship, captain in captains.items(): print(f"The {ship} is captained by {captain}.")

c)

for item in captains.items(): print(f"The [ship] is captained by [captain].")