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




