Lab 10: Knowledge Check
These questions check your knowledge of concepts from this lab. Use the button at the bottom to show your answers once you've attempted them.
-
True or false: When you use a
forloop with a dictionary, the loop variable will only contain a key from the dictionary, and values must be accessed separately.
True False -
True or false: If you have a dictionary within a dictionary, you can access a value in the inner dictionary by using indexing the outer dictionary twice, e.g.,
outer[outerKey][innerKey].
True False -
If we run the following code, what is final the value of the variable
taxes?taxes = { 'income': 50000 } taxes['taxable'] = taxes['income'] taxes['income'] += 10000 taxes['tax'] = taxes['taxable'] * 0.1
- { 'income': 50000, 'taxable': 50000, 'tax': 5000 }
- { 'income': 60000, 'taxable': 50000, 'tax': 5000 }
- { 'income': 60000, 'taxable': 60000, 'tax': 5000 }
- { 'income': 60000, 'taxable': 60000, 'tax': 6000 }
-
You are working with code for a contacts app that stores names, phone numbers, and email addresses. The data is stored as a dictionary where names are the keys, and the values are inner dictionaries that have keys like 'phone', 'email', etc. What are some issues that might cause problems with this approach? (choose all that apply) (pick one or more)
- There would be problems if two people have the same name as each other.
- There would be problems if two people had the same phone number.
- The outer dictionary might not be big enough to fit someone's contacts if they had a lot of contacts.
- The code for changing someone's name would be much more complex than the code for changing someone's phone number.
- If someone doesn't have a phone number, it will be impossible to create a contact entry for them.
-
Which of the following code snippets are correct ways to loop over a dictionary
Dsuch that within the loop,keyholds a key andvalueholds the associated value? (pick one or more)
-
for key in D: value = D ... -
for value in D: key = D.get(value) ... -
for key in D: value = D[key] ... -
for key, value in D: ... -
for key, value in D.items(): ...
-
-
Which of the following statements are true about dictionaries? (pick one or more)
-
You can use
.appendto add key/value pairs. -
You can use
.insertto add key/value pairs. - You can use square brackets to access the value associated with a particular key.
-
You can use
.getto access the value associated with a particular key. -
You can use
.popto remove a key/value pair if you only know the value. -
The
inoperator checks keys and values. -
You can use
dict[key] = valueto assign a value to a dictionary slot specified using a key, even if that key isn't in the dictionary yet.
-
You can use
Table of Contents
- Lab 10 Home
- Dictionary Lecture Notebook
- Cheat Sheet: Lists & Dictionaries
- Ironman Data Exercises
- Knowledge Check