Code Reading Exercises¶
Overview¶
These exercises develop a skill that many courses neglect: reading and understanding code written by someone else. In professional work you spend far more time reading code than writing it. Each exercise gives you a complete program and asks you to analyze it, find issues, extend it, and test it.
Prerequisites¶
Complete Level 2 before attempting these exercises. You should be comfortable with functions, files, lists, dicts, and basic error handling.
Exercises¶
| # | Exercise | What you practice |
|---|---|---|
| 01 | Mystery Function | Trace execution, infer purpose, write docstrings and tests |
| 02 | Find the Bugs | Identify 4 subtle defects in a data processing pipeline |
| 03 | Add a Feature | Understand a working TODO app, then extend it with 3 features |
How to Work Through Each Exercise¶
- Read the code in
codebase.pywithout running it. Take notes. - Answer the comprehension questions in
README.md. - Complete the tasks described in the README (trace, fix, extend, test).
- Run
python -m pytest tests/to verify your work.
How to Approach Code Reading¶
- Skim first. Read the imports, class/function names, and docstrings (if any). Get the shape of the code before reading line by line.
- Trace with real data. Pick a concrete input and follow it through the code on paper or in your head.
- Ask "why?" at every branch. Every
if,for, andtryexists for a reason. What case does it handle? - Look for patterns. Is this a pipeline? A state machine? A recursive processor? Naming the pattern helps you predict what comes next.
- Note what confuses you. Confusion is signal. The confusing part is where the interesting logic lives.
Why Code Reading Matters¶
- You will join teams with thousands of lines of existing code.
- Debugging starts with reading, not writing.
- Code reviews require you to understand code you did not write.
- Learning from others' code accelerates your own skill growth.