Part IV: Building Robust Programs
There is a difference between code that works and code you can trust.
Code that works runs fine on your machine, with your test data, on a Tuesday afternoon. Code you can trust handles the unexpected: a missing file, a user who types "banana" where you expected a number, a colleague who modifies your function without understanding what it does. Part IV teaches you to write the second kind.
What This Part Covers
Chapter 11: Error Handling confronts reality: things go wrong. Files disappear, users enter garbage, network connections fail, and data arrives in formats you did not anticipate. Instead of letting your programs crash with an ugly traceback, you will learn to anticipate failures, catch exceptions gracefully, and give users helpful feedback. You will also learn Python's distinctive EAFP philosophy --- "Easier to Ask Forgiveness than Permission" --- a mindset that will change how you approach defensive programming.
Chapter 12: Modules and Packages teaches you to organize code at scale. A single-file script works for small problems, but real projects have thousands of lines spread across dozens of files. You will learn to split your code into modules, bundle them into packages, and leverage Python's extraordinary standard library. When you discover that collections.Counter replaces fifteen lines of counting code you wrote by hand, you will understand why experienced developers say "don't reinvent the wheel."
Chapter 13: Testing and Debugging is where you learn to prove your code works --- not by running it once and hoping, but by writing automated tests that verify behavior every time. You will write unit tests with pytest, practice test-driven development (TDD), and learn systematic debugging strategies that replace the "stare at the screen and guess" approach. Testing is not glamorous. It is what separates professional software from homework assignments.
Why This Matters
In industry, the code you ship is only as good as your error handling, your test coverage, and your code organization. A feature that works but crashes on edge cases costs the company money and costs users trust. A codebase that nobody can navigate slows down every developer who touches it. A function that nobody has tested is a function that nobody trusts. The skills in Part IV are what employers mean when they say they want someone who "writes production-quality code."
What You Will Be Able to Do
By the end of Part IV, you will be able to:
- Handle exceptions gracefully using
try/except/else/finally - Raise custom exceptions with meaningful error messages
- Organize code into modules and packages with clean imports
- Navigate and leverage the Python standard library
- Write unit tests using
pytestand apply test-driven development - Debug systematically using binary search, print debugging, and a debugger
- Measure code coverage and understand its limitations
How the Chapters Connect
Chapter 11 teaches you to handle failure. Chapter 12 teaches you to organize success. Chapter 13 teaches you to verify both. They form a natural progression from defensive coding to architectural thinking to quality assurance. Together, they transform your code from "scripts that run" into "software you can maintain."
Your Progressive Project: TaskFlow
TaskFlow reaches version 1.0 in this part --- and the version number is no accident. In Chapter 11, you will add robust error handling for every user input, every file operation, and every edge case, so TaskFlow never crashes with a raw traceback (v1.0). In Chapter 12, you will split TaskFlow into a proper package with separate modules: models.py, storage.py, display.py, and cli.py (v1.1). In Chapter 13, you will write a full test suite with pytest that verifies adding, deleting, searching, saving, and loading tasks (v1.2).
When you finish Part IV, TaskFlow will not just work. You will be able to prove it works. That is the professional standard.