← 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
Ready to go further?

These free tutorials are a preview, our Backend Development cohort covers it in depth, with a real project and a cohort alongside you. Active

Enroll in Backend Development
Don't miss out

Ready to build something real?

Create a free account to enroll in a track, and get notified the moment a cohort opens.

Create free account