Module 01 · Foundation

Start with a dictionary

Read, access, and change named values in a Python dictionary.

Before you start: You only need to recognize text, numbers, and the print function.

Python loads from this site on your first run.

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))
Verified example output · pandas 2.2.3
{'Name': 'Amira', 'Age': 20, 'Campus': 'RAK'}
dict
3

Watch for this. Do not mix up braces { } for dictionaries with brackets [ ] used for lists and access.

Your turn

Create a dictionary for Bilal, age 21, at Dubai. Add a fourth key called Program with value IT. Print the dictionary and its number of pairs.

For the code check, keep these result variable names: student. Different valid approaches are welcome.

Show a hint

Use four key: value pairs inside one pair of curly brackets.

Show one worked solution
student = {"Name": "Bilal", "Age": 21, "Campus": "Dubai", "Program": "IT"}
print(student)
print(len(student))

The result has four pairs. Program is a new key, so the dictionary length increases to 4.

Compare your explanations
  1. Read. "Age" is the key. The integer 20 is its value.
  2. Predict. It prints 3, the number of key-value pairs.
  3. 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.

Read the example

student = {"Name": "Amira", "Age": 20, "Campus": "RAK"}
print(student["Name"])
print(student["Age"])
print(student.get("Email", "Not provided"))
Verified example output · pandas 2.2.3
Amira
20
Not provided

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.

Show one worked solution
student = {"Name": "Amira", "Age": 20, "Campus": "RAK"}
campus = student["Campus"]
program = student.get("Program", "Undeclared")
has_age = "Age" in student
print(campus)
print(program)
print(has_age)

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
  1. Read. It asks for the value associated with the exact key "Name".
  2. Predict. Amira, then 20, then Not provided, each on a separate line.
  3. 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.

Read the example

student = {"Name": "Amira", "Age": 20, "Campus": "RAK"}
student["Age"] = 21
student["Program"] = "AI"
removed = student.pop("Campus")
print(student)
print("Removed:", removed)
Verified example output · pandas 2.2.3
{'Name': 'Amira', 'Age': 21, 'Program': 'AI'}
Removed: RAK

Watch for this. pop() raises KeyError for an absent key unless you also provide a default.

Your turn

Start with the same original dictionary. Increase Age by 2, add Program as IT, and remove Campus. Print the final dictionary.

For the code check, keep these result variable names: student. Different valid approaches are welcome.

Show a hint

Read the existing age on the right side of the assignment: student["Age"] + 2.

Show one worked solution
student = {"Name": "Amira", "Age": 20, "Campus": "RAK"}
student["Age"] = student["Age"] + 2
student["Program"] = "IT"
student.pop("Campus")
print(student)

Age is 22 and Program is IT. Campus is no longer a key. Reading the old age lets the same calculation work for other starting ages.

Compare your explanations
  1. Read. student["Age"] = 21 changes Age. student["Program"] = "AI" adds Program.
  2. Predict. Three: Name, Age, and Program.
  3. 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.

Show one worked solution
student = {"Name": "Amira", "Age": 20, "Campus": "RAK", "Program": "AI"}
print(list(student.keys()))
pairs = list(student.items())
print(pairs)

There are four keys and four pairs. Each pair is displayed as a tuple, such as ("Program", "AI").

Compare your explanations
  1. Read. One dictionary key and the value associated with that key.
  2. Predict. Three times, once for each pair.
  3. Explain. The first checks keys and is False. The second checks values and is True.

One quick check

Which method gives key-value pairs?

What you should be able to explain. I can inspect a dictionary and explain what a key-value loop does.

Further detail: official documentation for this topic.