Python for a Coding Assessment
A refresher path through the Python graph for someone who has written Python before and has an assessment coming: run and read, rebuild the core, then work the way an assessment expects.
I can iterate over a collection with a loop or a comprehension, and use a generator when the data should not all be held in memory at once.
Walking over data and producing new data from it: the shape most Python code takes once it does anything real.
In scope: for loops, comprehensions for lists, dicts and sets, iterating a dictionary properly,
and generators for data too large or too slow to materialise. Out of scope: itertools, and
parallelism.
A comprehension is a loop whose whole purpose is to build a collection. When that is what the loop does, the comprehension says so in one line:
names = [user.name for user in users if user.active]When the loop does several things — building, logging, updating a counter — a comprehension hides that, and a plain loop is clearer. The test is not brevity. It is whether one line describes the result or conceals the work.
Iterating a dict gives keys. .values() gives values, .items() gives both as pairs. Choosing the
right one removes the most common noise in beginner code: looking up d[key] inside a loop that is
already holding the value.
A function that yields returns a generator: nothing runs until something asks for the next item,
and only one item exists at a time. Two things follow.
The first is memory. Reading a ten-million-line file into a list needs the whole file; a generator needs one line. The second is when work happens — a generator that is never consumed does nothing at all, which is either exactly what you wanted or a bug that looks like missing output.
A generator is exhausted once consumed. Looping over it twice gives the items and then nothing, with no error. If you need the data twice, keep a list.
len() on
one is an error, not a slow path.for i in range(len(items)) is how you loop.” Iterate the items directly, and use
enumerate when you genuinely need the index.Have learners write the same transformation three ways — loop, comprehension, generator — over a file big enough to feel, then ask which they would put in a pull request and why. The answer should mention the reader, not the benchmark.
The exhaustion trap is worth meeting on purpose: loop over a generator twice, watch the second loop produce nothing silently, then explain it.
| Resource | Kind | Cost | Provider |
|---|---|---|---|
| The Python Tutorial: data structures, including list comprehensions | reading | free | Python Software Foundation |
| Functional programming HOWTO: iterators and generators | reading | free | Python Software Foundation |
| Python Tutor, step-by-step execution | interactive | free | Python Tutor |
A refresher path through the Python graph for someone who has written Python before and has an assessment coming: run and read, rebuild the core, then work the way an assessment expects.
No assessment yet.
The holder can take an unfamiliar problem, write a tested Python solution someone else can run, and explain what it costs.
Nothing to show yet. The publisher last published on 2026-09-20, and no aggregate for this outcome has reached the 50 learners a number needs. Open Degree does not track learners. About learner evidence.