Module 1 · Python · Drills
Reading is not knowing. Type every one of these yourself — in the REPL or a file — before you reveal the solution. Effortful recall is the point.
Drill 1 strings
Given full = "Sam Ahmed", print it as "Ahmed, Sam" using a split and an f-string.
full = "Sam Ahmed" first, last = full.split() print(f"{last}, {first}") # Ahmed, Sam
Drill 2 dict
Make a dict for a document with keys title and pages. Then safely print the author key (which doesn't exist) without crashing.
doc = {"title": "Intro", "pages": 12}
print(doc.get("author")) # None
print(doc.get("author", "Unknown")) # Unknown (default)
Drill 3 list + loop
Given nums = [4, 1, 7, 1, 4, 9], print only the unique values, and print how many there are.
nums = [4, 1, 7, 1, 4, 9] unique = set(nums) print(unique) # {1, 4, 7, 9} print(len(unique)) # 4
Drill 4 slicing
Given title = "Quarterly Report 2026", print just the year using a slice (not split).
title = "Quarterly Report 2026" print(title[-4:]) # 2026 (last four characters)
Negative indices count from the end; [-4:] means "from 4-before-the-end to the end".
Drill 5 enumerate
Print a numbered list of ["a.pdf", "b.pdf", "c.pdf"] starting at 1, like 1. a.pdf.
docs = ["a.pdf", "b.pdf", "c.pdf"] for i, doc in enumerate(docs, start=1): print(f"{i}. {doc}")
tag_counter.py: given a list of documents (each a dict with a tags list), count how many documents carry each tag and print them most-common first. This is the exact shape of analytics code you'll write on the job.
Build · tag counter
Input:
docs = [
{"title": "A", "tags": ["ai", "uae"]},
{"title": "B", "tags": ["ai"]},
{"title": "C", "tags": ["uae", "jobs"]},
]
counts = {}
for doc in docs:
for tag in doc["tags"]:
counts[tag] = counts.get(tag, 0) + 1
for tag, n in sorted(counts.items(), key=lambda kv: kv[1], reverse=True):
print(f"{tag}: {n}")
# ai: 2 / uae: 2 / jobs: 1
Note the nested loop: documents on the outside, their tags on the inside. This pattern — iterate a collection of records, accumulate into a dict — is half of all data code.
Click a card to flip it. Say the answer out loud before you flip — that's the rep that builds storage strength.
len(my_list) — works on any collection or string.null?None (capital N). Check with x is None.xs?xs[-1] — negative indexing counts from the end.["a","b"] into "a-b"?"-".join(["a","b"]) — join is a method on the separator.for i, x in enumerate(xs):s.upper() (and s.lower(), s.strip()).Tick each only if you can do it without looking:
enumerate) and write an if/elif/else