Introduction
It’s important to be methodical when programming, and in this chapter we’ll see how best to structure your Python code. Following this structure takes much of the guesswork out of programming. Many questions about where certain elements of your program belong are already answered for you. What’s presented here is based on common (indeed nearly universal) practice for professionally written code.
We’ll also learn a little bit about how to proceed when writing code (that is, in small, incremental steps), how to test your code, how to use assertions, and what to do about the inevitable bugs.
Learning objectives
- You will learn about incremental development, and how to use comments as “scaffolding” for your code.
- You will learn how to organize and structure your code.
- You will understand how Python handles the main entry point of your program, and how Python distinguishes between modules that are imported and modules that are to be executed.
- You will be able to write code with functions that can be imported and used independently of any driver code.
- You will understand how to test your code, and when to use assertions in your code.
Terms and Python keywords introduced
assert
(Python keyword) and assertionsAssertionError
- bug
- driver code
- dunder
- entry point and top-level code environment
- incremental development
- namespace
- rubberducking
Copyright © 2023–2025 Clayton Cafiero
No generative AI was used in producing this material. This was written the old-fashioned way.