Python: Do This, Not That!

Preface

This book is a compact guide to writing clear, idiomatic Python. Each chapter uses a small example to contrast a clumsy approach with a cleaner one, with an emphasis on readability first and performance second.

The intended audience is Python developers who already know the language basics and want sharper instincts for everyday code review decisions. The examples are deliberately short, but the underlying goal is practical: write code that is easier to read, easier to change, and less error-prone.

Under each major section, you will see two sub-sections: Don't Do This and Do This. Treat them as rules of thumb, not absolute laws. Python is full of tradeoffs, and the “better” choice depends on context. In this edition, the examples target modern Python 3 and favor current best practices over historical Python 2 compatibility.

Ideas and code examples are adapted from [YaK, RaH, JeK] and from the wider Python community.

To follow along, you only need a recent Python 3 installation and a way to run short scripts or a REPL. A virtual environment is recommended.

python3 -m venv .venv
source .venv/bin/activate

You can then paste the snippets into python or save them to small files as you work through the chapters.

The diagram below shows the overall shape of the book: it starts with everyday coding habits, then moves into data and file handling, and finally closes with version-specific patterns from newer Python releases.

That flow mirrors the intent of the book: sharpen judgment on common review comments first, then apply the same “do this, not that” mindset to more specialized and version-dependent features.

The chapters are grouped into core patterns first, followed by patterns that depend on newer Python versions.

Core Patterns

Version-Specific Patterns

About

One-Off Coder logo.

One-Off Coder is an educational, service and product company. Please visit us online to discover how we may help you achieve life-long success in your personal coding career or with your company’s business goals and objectives.

Bibliography

[YaK]

Intermediate python. https://book.pythontips.com/en/latest/index.html. Accessed: 2019-10-18.

[RaH]

Transforming code into beautiful, idiomatic python. https://www.youtube.com/watch?v=OSGv2VnC0go. Accessed: 2019-10-18.

[JeK]

Writing idiomatic python. https://jeffknupp.com/writing-idiomatic-python-ebook/. Accessed: 2019-10-18.

Author

Jee Vang, Ph.D.