Module 1 · Python · Drills

Drills: Python Foundations

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.

How to use this page Each drill is a small task. Attempt it first, run it, then click “Show solution” to compare. If yours works differently but correctly — great, that's fluency. Tick each box as you go; your progress is saved in this browser.

A · Warm-up reps Basic

Drill 1 strings

Given full = "Sam Ahmed", print it as "Ahmed, Sam" using a split and an f-string.

Show solution
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.

Show solution
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.

Show solution
nums = [4, 1, 7, 1, 4, 9]
unique = set(nums)
print(unique)        # {1, 4, 7, 9}
print(len(unique))   # 4

B · Stretch Intermediate

Drill 4 slicing

Given title = "Quarterly Report 2026", print just the year using a slice (not split).

Show solution
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.

Show solution
docs = ["a.pdf", "b.pdf", "c.pdf"]
for i, doc in enumerate(docs, start=1):
    print(f"{i}. {doc}")

C · Build challenge Build

Mini-project Write 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"]},
]
Show solution
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.

D · Rapid recall Flashcards

Click a card to flip it. Say the answer out loud before you flip — that's the rep that builds storage strength.

How do you get a list's length?
len(my_list) — works on any collection or string.
click to flip
Python's word for null?
None (capital N). Check with x is None.
click to flip
Last item of a list xs?
xs[-1] — negative indexing counts from the end.
click to flip
Join ["a","b"] into "a-b"?
"-".join(["a","b"]) — join is a method on the separator.
click to flip
Loop with the index too?
for i, x in enumerate(xs):
click to flip
Make a string uppercase?
s.upper() (and s.lower(), s.strip()).
click to flip

E · Self-check before moving on

Tick each only if you can do it without looking:

Next All ticked? You have the raw material of Python. Next we make it reusable and organised: Lesson 1.2 — Functions, OOP & Modules.