Easy Python Dictionary Practice for Beginners


🔹 Exercise 1: Create a Dictionary
Create a dictionary named student
with the following key-value pairs:
name: "John"
age: 20
course: "Computer Science"
Then print the dictionary.
🔹 Exercise 2: Access and Update Values
Using the student
dictionary from Exercise 1:
Access and print the value for the key
"name"
.Change the value of
"age"
to21
.
🔹 Exercise 3: Add and Remove Items
Using the same student
dictionary:
Add a new key
"grade"
with value"A"
.Remove the key
"course"
from the dictionary.
🔹 Exercise 4: Loop Through Dictionary
Write a program to print all keys and values in the student
dictionary using a for
loop.
🔹 Exercise 5: Nested Dictionary
Create a dictionary called classroom
with 2 students, each having a nested dictionary:
{
"student1": {"name": "Alice", "age": 22},
"student2": {"name": "Bob", "age": 23}
}
Print the name of student2
.
✅ Answers
✅ Exercise 1:
student = {
"name": "John",
"age": 20,
"course": "Computer Science"
}
print(student)
✅ Exercise 2:
print(student["name"]) # Output: John
student["age"] = 21
✅ Exercise 3:
student["grade"] = "A"
del student["course"]
✅ Exercise 4:
for key, value in student.items():
print(key, ":", value)
✅ Exercise 5:
classroom = {
"student1": {"name": "Alice", "age": 22},
"student2": {"name": "Bob", "age": 23}
}
print(classroom["student2"]["name"]) # Output: Bob
Subscribe to my newsletter
Read articles from Arzath Areeff directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Arzath Areeff
Arzath Areeff
I co-founded digizen.lk to promote online safety and critical thinking. Currently, I’m developing an AI app to fight misinformation. As Founder and CEO of ideaGeek.net, I help turn startup dreams into reality, and I share tech insights and travel stories on my YouTube channels, TechNomad and Rz Omar.