codegurukool.com · Free coding tutorials & online compiler

Home › Python Tutorial › Python dictionaries

Python dictionaries

Key-value pairs.

A dictionary maps keys to values. Use [key] to read or set, get for a safe read with a default, and items() to loop over keys and values together.

Example code

p = {"name": "Ada", "age": 36}
p["city"] = "London"
print(p)
print(p.get("zip", "n/a"))
for k, v in p.items():
    print(k, v)

Run this code in the online editor

← Python tuples and setsPython if...else →