Quizz
The full text of this chapter, to read or revisit. To practise, do the exercises.
βΆ Do the exercisesExercise 1 β Quiz: Valid Dictionary Syntax
Which of the following creates a valid dictionary?
codemon = {"name": "Aquachu", "level": 15}codemon = ["name": "Aquachu", "level": 15]codemon = {name: Aquachu, level: 15}codemon = ("name": "Aquachu", "level": 15)Hint : Dictionaries use curly braces {} and keys must be strings in quotes.
Exercise 2 β Quiz: Create a Dictionary
Complete the code to create a dictionary with two key-value pairs.
Hint : Remember: variable = {"key": value, "key2": value2}
Exercise 3 β Quiz: Dictionary Values
What types of values can a dictionary contain?
Hint : Think about what we've seen in dictionaries - can they hold lists, booleans, and other types?
Exercise 4 β Quiz: Dictionary Terminology
Match each term with its definition:
{}Exercise 5 β Quiz: Duplicate Keys
What happens if you create a dictionary with duplicate keys?
codemon = {"type": "fire", "type": "water"}
print(codemon){"type": "water"}{"type": "fire"}Hint : Dictionary keys must be unique - what happens when you redefine a key?
Exercise 6 β Quiz: Accessing Values
Complete the code to access the "name" value from the dictionary.
Hint : Use square brackets with the key name in quotes.
Exercise 7 β Quiz: .get() vs [] Access
What's the advantage of using .get() instead of [] to access dictionary values?
.get() doesn't raise an error if the key doesn't exist.get() is faster.get() can modify valuesHint : Think about what happens when you try to access a key that doesn't exist with each method.
Exercise 8 β Quiz: Updating Values
Complete the code to change the level to 25.
Hint : Use the same syntax as accessing, but with the assignment operator.
Exercise 9 β Quiz: Adding New Keys
How do you add a new key-value pair to an existing dictionary?
codemon["hp"] = 100codemon.add("hp", 100)codemon.insert("hp", 100)codemon.append({"hp": 100})Hint : It's the same syntax as modifying - just use a key that doesn't exist yet!
Exercise 10 β Quiz: Checking Key Existence
What does "level" in codemon check?
"level" exists as a key"level" exists as a value"level" is greater than 0Hint : The in operator checks keys by default, not values.
Exercise 11 β Quiz: Removal Methods
Match each removal method with what it does:
del dict[key]dict.pop(key)dict.popitem()dict.clear()Exercise 12 β Quiz: Using del
What happens if you use del on a key that doesn't exist?
KeyError is raisedNone is returnedHint : del is not safe - it will complain if the key doesn't exist!
Exercise 13 β Quiz: Safe Removal with pop()
Complete the code to safely remove "trainer" with a default value of "Unknown".
Hint : Syntax: .pop(key, default_value)
Exercise 14 β Quiz: popitem() Behavior
What does .popitem() return?
(key, value)Hint : .popitem() removes and returns the last added pair as a tuple.
Exercise 15 β Quiz: After clear()
What does a dictionary look like after calling .clear()?
{}None[]Hint : .clear() removes all items but keeps the dictionary itself.
Exercise 16 β Quiz: Get All Keys
Complete the code to get all dictionary keys.
Hint : There's a method specifically for getting all keys!
Exercise 17 β Quiz: Checking Values
How do you check if a value exists in a dictionary?
"water" in codemon.values()"water" in codemon"water" in codemon.keys()codemon.has_value("water")Hint : By default, in checks keys. To check values, use .values().
Exercise 18 β Quiz: What .items() Returns
What format does .items() return key-value pairs in?
(key, value)[key, value]{key: value}"key: value"Hint : .items() returns pairs as tuples that can be unpacked.
Exercise 19 β Quiz: Method Purposes
Match each method with what it returns:
.keys().values().items().get(key)Exercise 20 β Quiz: Default 'in' Behavior
By default, what does the in operator check in a dictionary?
Hint : "name" in dict checks if "name" is a key.
Exercise 21 β Quiz: Basic Dictionary Loop
Complete the code to loop through dictionary keys by default.
Hint : Use a standard for loop - Python will iterate over keys by default!
Exercise 22 β Quiz: Iterating Over Values
Which loop iterates over dictionary values only?
for value in dict.values():for value in dict:for value in dict.keys():for value in dict.items():Hint : To get only values, you need to explicitly use .values().
Exercise 23 β Quiz: Unpacking Key-Value Pairs
Complete the code to loop through both keys and values.
Hint : Use .items() and unpack into two variables!
Exercise 24 β Quiz: Summing Values
What's the best way to calculate the sum of all dictionary values?
.values() and add each one.keys() and add each one.items() and add keysHint : You only need the values to sum them, not the keys!
Exercise 25 β Quiz: When to Use Each Method
Match each scenario with the best iteration method:
.values().keys() or in.items().items() with if