Quizz

The full text of this chapter, to read or revisit. To practise, do the exercises.

β–Ά Do the exercises

Exercise 1 β€” Quiz: Valid Dictionary Syntax

Which of the following creates a valid dictionary?

Options
β€’ 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?

Options
β€’ Any data type (strings, numbers, lists, etc.)
β€’ Only strings
β€’ Only numbers
β€’ Only strings and numbers

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:

Items to match
β€’ Key
β€’ Value
β€’ Key-Value Pair
β€’ Empty Dictionary
Definitions
β€’ The identifier used to access a value
β€’ The data stored with a key
β€’ One complete entry in a dictionary
β€’ {}

Exercise 5 β€” Quiz: Duplicate Keys

What happens if you create a dictionary with duplicate keys?

codemon = {"type": "fire", "type": "water"} print(codemon)
Options
β€’ Only the last value is kept: {"type": "water"}
β€’ Both values are kept as a list
β€’ An error is raised
β€’ Only the first value is kept: {"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?

Options
β€’ .get() doesn't raise an error if the key doesn't exist
β€’ .get() is faster
β€’ .get() can modify values
β€’ There is no difference

Hint : 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?

Options
β€’ codemon["hp"] = 100
β€’ codemon.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?

Options
β€’ If "level" exists as a key
β€’ If "level" exists as a value
β€’ If "level" is greater than 0
β€’ If the dictionary is empty

Hint : The in operator checks keys by default, not values.

Exercise 11 β€” Quiz: Removal Methods

Match each removal method with what it does:

Items to match
β€’ del dict[key]
β€’ dict.pop(key)
β€’ dict.popitem()
β€’ dict.clear()
Definitions
β€’ Remove a key without returning its value
β€’ Remove a key and return its value
β€’ Remove and return the last key-value pair
β€’ Remove all items from the dictionary

Exercise 12 β€” Quiz: Using del

What happens if you use del on a key that doesn't exist?

Options
β€’ A KeyError is raised
β€’ Nothing happens
β€’ None is returned
β€’ The dictionary is cleared

Hint : 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?

Options
β€’ A tuple containing (key, value)
β€’ Just the key
β€’ Just the value
β€’ A list of all remaining keys

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()?

Options
β€’ {}
β€’ None
β€’ []
β€’ The dictionary is deleted

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?

Options
β€’ "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?

Options
β€’ Tuples: (key, value)
β€’ Lists: [key, value]
β€’ Dictionaries: {key: value}
β€’ Strings: "key: value"

Hint : .items() returns pairs as tuples that can be unpacked.

Exercise 19 β€” Quiz: Method Purposes

Match each method with what it returns:

Items to match
β€’ .keys()
β€’ .values()
β€’ .items()
β€’ .get(key)
Definitions
β€’ All the keys
β€’ All the values
β€’ All key-value pairs as tuples
β€’ A single value (or None)

Exercise 20 β€” Quiz: Default 'in' Behavior

By default, what does the in operator check in a dictionary?

Options
β€’ Keys only
β€’ Values only
β€’ Both keys and values
β€’ The dictionary size

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?

Options
β€’ 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?

Options
β€’ Loop through .values() and add each one
β€’ Loop through .keys() and add each one
β€’ Loop through .items() and add keys

Hint : 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:

Items to match
β€’ Calculate total of all numbers
β€’ Check if certain keys exist
β€’ Display formatted key: value pairs
β€’ Filter items based on conditions
Definitions
β€’ .values()
β€’ .keys() or in
β€’ .items()
β€’ .items() with if
Back to the course