Each run starts with fresh data. Download any exported files before another run. Your written answers and code drafts are saved on this device; export your work to keep a backup.
M01C01 · Explore · predict · practice
What are the key and the value?
A dictionary stores pairs: a key and its value. The key is a name you use to find a value, such as "Name" or "Age". Curly brackets create the dictionary; a colon separates each key from its value. Here, one dictionary describes one fictional student.
Read the example
# Text uses quotes. A whole number does not need quotes.
student = {"Name": "Amira", "Age": 20, "Campus": "RAK"}
print(student)
print(type(student).__name__)
print(len(student))
The result has four pairs. Program is a new key, so the dictionary length increases to 4.
Compare your explanations
Read. "Age" is the key. The integer 20 is its value.
Predict. It prints 3, the number of key-value pairs.
Explain. "20" is text; 20 is an integer. Numbers stored as text need conversion before arithmetic.
One quick check
What does the colon do in "Age": 20?
What you should be able to explain. I can identify the keys, values, and number of pairs in a dictionary.
M01C02 · Explore · predict · practice
Find a value by its key
Square brackets after a dictionary look up a key. The key must match exactly, including upper and lower case. get() is useful when a key might be missing: you can supply a default value instead of raising KeyError.
Watch for this. A default from get() does not insert the missing key into the dictionary.
Your turn
Store the campus in campus. Use get() to request Program with the default "Undeclared" and save the value in program. Store whether "Age" is a key in has_age. Print all three values.
For the code check, keep these result variable names: campus, has_age, program. Different valid approaches are welcome.
Show a hint
Use student["Campus"], student.get(...), and "Age" in student.
Campus is RAK. Program is missing, so get() returns the chosen default. Membership with in checks keys, so has_age is True.
Compare your explanations
Read. It asks for the value associated with the exact key "Name".
Predict. Amira, then 20, then Not provided, each on a separate line.
Explain. No. The key is "Name", with an uppercase N. The different key "name" is absent, so direct access raises KeyError.
One quick check
Which expression safely returns "Unknown" when Phone is missing?
What you should be able to explain. I can choose between direct key access and a safe lookup with a default.
M01C03 · Explore · predict · practice
Change, add, and remove a pair
Assigning a value to an existing key replaces its old value. Assigning to a new key adds a pair. pop() removes a key and returns its value. A dictionary cannot hold two separate entries with the same key.
Explain. Age already exists. Assignment replaces its value instead of adding a second Age key.
One quick check
What happens when you assign another value to an existing key?
What you should be able to explain. I can update a dictionary without confusing replacement with insertion.
M01C04 · Explore · predict · practice
Read all keys and all pairs
keys() gives the keys, values() gives their values, and items() gives key-value pairs. list() turns these views into lists that are easy to display. A for loop visits each pair in turn; the indented line runs once per pair.
Read the example
student = {"Name": "Amira", "Age": 20, "Campus": "RAK"}
print(list(student.keys()))
print(list(student.values()))
for key, value in student.items():
print(key, "=", value)
Verified example output · pandas 2.2.3
['Name', 'Age', 'Campus']
['Amira', 20, 'RAK']
Name = Amira
Age = 20
Campus = RAK
Watch for this. A Python dictionary preserves insertion order, but this does not mean the keys are alphabetically sorted.
Your turn
Add Program as AI, print only the keys, and build a list named pairs using list(student.items()). Print pairs.
For the code check, keep these result variable names: pairs. Different valid approaches are welcome.
Show a hint
items() exposes both parts of each dictionary entry together.