Write Python. Understand every error.

Open the editor

Cheat sheets

Dictionaries cheat sheet

Key and value pairs. Each example shows the code and exactly what it prints.

d = {"Riya": 90, "Arjun": 75}
print(d["Riya"])
90
Look up a value
d = {"a": 1}
d["b"] = 2
print(d)
{'a': 1, 'b': 2}
Add or change a key
d = {"a": 1}
print(d.get("z", 0))
0
Look up with a default
d = {"a": 1, "b": 2}
print(list(d.keys()), list(d.values()))
['a', 'b'] [1, 2]
Keys and values
d = {"a": 1, "b": 2}
for k, v in d.items():
    print(k, v)
a 1
b 2
Loop over pairs
d = {"a": 1, "b": 2}
print("a" in d, len(d))
True 2
Membership and size
c = {}
for ch in "hello":
    c[ch] = c.get(ch, 0) + 1
print(c)
{'h': 1, 'e': 1, 'l': 2, 'o': 1}
Counting pattern
d = {"a": 1, "b": 2}
del d["a"]
print(d)
{'b': 2}
Delete a key

Practise these in the Py-CRAFT editor.

Py-CRAFT cheat sheet. © 2026 Dr. Saptarshi Das (PhD, IIT Kharagpur | ME, Jadavpur University). All rights reserved.