Dictionary in Python
A dictionary stores key-value pairs and provides fast lookup by unique key.
What is Dictionary?
A dictionary stores key-value pairs and provides fast lookup by unique key.
Create, update, and iterate over a Python dictionary.
When should you use it?
- Learn Python syntax through a practical example.
- Build a foundation for larger programming problems.
- Test an idea quickly without a local setup.
Example code
main.py
student = {"name": "Ada", "score": 92}
student["course"] = "Python"
for key, value in student.items():
print(f"{key}: {value}")
Expected output
name: Ada
score: 92
course: Python
How it works
Square brackets add or update a value. The items method returns each key and value for iteration.
Change the values and run the program in the CodeUtility online Python compiler without installing Python locally.
Practice exercises
Modify the runnable example with the exercises below to build understanding beyond copying the result.
- Change the input and predict the output before running.
- Handle empty or invalid input.
- Wrap the logic in a function and add more test cases.