Store and access data as key-value pairs with Python dictionaries.
1student = {2 "name": "Alice",3 "age": 21,4 "major": "Computer Science",5 "gpa": 3.86}7 8# Access by key9print(student["name"]) # Alice10 11# Safe access with .get()12print(student.get("email", "Not found")) # Not found13 14# Add/update15student["email"] = "alice@example.com"16student["gpa"] = 3.917 18print(student)Alice
Not found
{'name': 'Alice', 'age': 21, 'major': 'Computer Science', 'gpa': 3.9, 'email': 'alice@example.com'}1scores = {"math": 95, "science": 88, "english": 92}2 3# Loop through keys4for subject in scores:5 print(subject)6 7# Loop through values8for score in scores.values():9 print(score)10 11# Loop through key-value pairs12for subject, score in scores.items():13 print(f"{subject}: {score}")math science english 95 88 92 math: 95 science: 88 english: 92