Tsallaka zuwa abun ciki
Justice Tech School

Dictionaries

A dictionary maps keys to values. Read with square brackets or .get() which returns None instead of failing when a key is missing.

Example
user = {"name": "Ada", "age": 21}
print(user["name"])
print(user.get("city", "unknown"))
user["city"] = "Lagos"
print(user)

Result

Ada
unknown
{'name': 'Ada', 'age': 21, 'city': 'Lagos'}

Exercise

Read the age.

user = {"age": 21}
print(user[])

Quiz Test

Why use .get()?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.