← Python tutorial

Python

Dictionaries

A dictionary groups values under named keys: student = {"name": "Ada", "track": "Frontend"}.

Example: two ways to read a value

student = {"name": "Ada", "track": "Frontend"}
print(student["name"], "is studying", student["track"])
print(student.get("age", "not provided"))
Ada is studying Frontend
not provided

student["name"] raises an error if the key doesn't exist. student.get("age", "not provided") instead returns a fallback value safely — no error — when the key is missing. Use bracket access when a key is guaranteed to be there; use .get() with a sensible default whenever it might not be, which is common when working with data that came from an external source like an API response.

Example
student = {"name": "Ada", "track": "Frontend"}
print(student["name"], "is studying", student["track"])
Output
Ada is studying Frontend