The Powerful Python Book

Read free on this page.
Or Purchase

  • Your book is absolutely amazing and your ability to articulate clear explanations for advanced concepts is unlike any I have ever seen in any Programming book. Thanks again for writing such a good and thought provoking book.
    Armando Someillan
  • A must have and must read for every Python developer.
    Jorge Carlos, Italy
  • I just wanted to let you know what an excellent book this is... It covers so many of the interview questions I'd got wrong previously.
    Fahad Qazi, London, UK.
  • Powerful Python is already helping me get huge optimization gains.
    Timothy Dobbins
  • Thanks. Keep up the good work. Your chapter on decorators is the best I have seen on that topic.
    Leon Tietz, Minnesota
  • A lot of advanced and useful Python features condensed in a single book.
    Giampaolo Mancini, Italy
  • What have I found good and valuable about the book so far? Everything honestly. The clear explanations, solid code examples have really helped me advance as a Python coder... Thank you! It has really helped me grasp some advanced concepts that I felt were beyond my abilities.
    Nick S., Colorado, USA
  • Only a couple chapters in so far but already loving the book. Generators are a game changer.
    Ben Randerson, Aberdeen, UK
  • LOVING Powerful Python so far.
    Gary Fritz, Colorado, USA
  • Thank you for your book. It is packed with great information, and even better that information is presented in an easy to understand manner.
    Jon Macpherson

Doing More with Python

I don’t need to tell you how amazing Python is. You know.

It’s still fun to recognize what an exciting time it is for Python. Depending on how you measure it, Python is one of the top languages in the world, if not THE top language…​ and it is only trending upward. More and more organizations are using Python…​ from tiny startups, to multinational corporations, to NASA and the JPL, and everything in between.

But enough cheerleading.

There are massive heaping piles of books for people new to Python, new to programming, or both. But you’re past the point where those do you much good. If you’ve been coding in Python for a while already, or Python is your second or third or seventh programming language…​ you need more.

Reading blog posts can help, as can studying open-source code, and (if you can swing it) working alongside a seasoned Pythonista. But these aren’t the most convenient ways to learn.

Hence, this book.

Python is richer than you imagine - as a language, as an ecosystem. For many engineering domains, it has grown into a truly outstanding choice for implementing high-quality, robust, maintainable software - everything from one-off scripts to sprawling, mission-critical applications. This book is designed to help you master all of that: to teach you techniques, patterns, and tools to permanently catapult your skill with everything Python has to offer.

To accomplish this, I did not hesitate to make hard choices. Understand this book is highly opinionated. I focus on certain topics, and certain ways of structuring code, because I believe it gives you the best payoff and value for your reading time.

Many blog posts have been written about different aspects of Python development; frankly, some of them are not good advice. My goal in this book is to give you excellent advice.

To that end, this book is practical. Everything herein is based on the lessons learned writing real-world software, usually as part of a team of engineers. That means factors like maintainability, robustness, and readability are considered more important than anything else. There is a balance between leveraging powerful abstractions, and writing code that is easy to reason about correctly by everyone on your team. Every page of this book walks that line.

Throughout, I give much attention to cognitive aspects of development. How do you write code which you and others can reason about easily, quickly, and accurately? This is one reason variable and function naming is important. But it goes far beyond that, to intelligently choosing which language features and library resources to use, and which to avoid.

Thus, this book is selective in its topics. It’s not too large, as measured by number of pages. That’s a feature, not a bug: you already have too much to read. The focus is on what’s most impactfully valuable to you, so that - as much as possible - everything you learn will serve you for years.

Sound good? Let’s begin.

Quick Note on The Book Edition

You are reading the "2.x" edition of the book. That is a work-in-progress update from the 2nd edition, on its way to the 3rd.

One big difference: the 2nd edition fully supported Python 2.7. But the third edition almost pretends Python 2 never existed.

Aside from cutting out obsolete Python-2 content, little will change. The principles of mind-blowingly effective Python development are surprisingly independent of the language version. Each new release refines and builds on the one before, yet does not change the solid essence of Python. That essence is what we focus on in this book.

On the other hand: since writing the 2nd edition, I have taught advanced Python to over 10,000 developers and data scientists worldwide…​ Written hundreds of articles for the Powerful Python newsletter…​ and become worlds wiser and more experienced as a developer and a communicator. As I edit this book, I bet I will think of completely new, marvelously better ways to present the same ideas.

We will see what happens.

Python Versions

This book aims to be timeless.

Wait, what? For a programming book?

Yes. And it is more possible than you might think.

Here’s the trick: The structure of quality Python software, and the patterns and strategies that work exceptionally well in the real world, are surprisingly independent of version.

This is truly fortunate; by learning these lasting concepts, you can easily adapt them to all future versions of Python, for years and years to come. And it is those slow-changing yet powerful principles we will focus on.

Most code examples in this book are written for Python 3.10 and later. With uncommon exceptions, most of the code will work fine in much older versions of Python. And they will likely not just run, but also be current and useful in far future versions as well.

How To Use This Book

At Powerful Python, we recognize two levels of learning.

The first is the information level. This is the level of learning where you read something, or I tell you something, and you memorize it. This puts facts, opinions, and other information in your mind, that you can recall later; parrot back to me; and even do some simple reasoning with it.

Which is great.

But there is a much deeper level of learning, called the ability level. The ability to do things you could not do before, when you are writing code.

Both are important…​ But the ability level is worlds more powerful.

There is no comparison, because the information level can be deceptive. It makes you feel like you understand something. But then you go to write code using it, staring at a blank IDE window, and you are stuck scratching your head…​ "Wait a second. How do I actually use this?"

Know that feeling? Every programmer does.

Reading alone can only ever teach you at the information level. It is up to you to transform that to ability-level learning. And you do that by putting what you read into practice.

Some ways to do that:

Type out code examples. This is practicing the syntax of the new code patterns. By itself, this does not allow you to fully generalize the feature to all the code you write. But what it does accomplish is teach you the indentation, grammar, and picky details of syntax you did not even realize were important. It also helps you memorize better, by focusing your attention more deeply. All this makes it easier to put into practice when you ARE writing code.

Which brings us to the next step:

Deliberately write new code using the feature. Look for opportunities to use what just read about. You even invent a small, toy program, specifically designed to exercise that particular Python feature.

Even better: when you are adding some feature to an existing program, see if you can directly use what read about. This is sometimes less straightforward; sometimes the code you need to write just does not need that specific language feature. But the whole point is to stretch your thinking…​ to practice looking for valuable ways to leverage new tools, in a manner that improves your code. And some topics - like unit testing - absolutely apply to EVERY program you write.

Bringing us to the next step:

Revisit working code. Have you ever read a book, waited a few years, read it again…​ and found that it seemed like a completely different book the second time?

Of course, the book did not change. YOU did. So you got different insights from that book, the second time you read it.

Something similar happens when you revisit old code.

Since the program is presumably working, you likely have a good understanding of the code base and the problems it is solving. And when you learn a new Python feature from this book, you can look at your code with fresh eyes…​ starting to think at higher levels of code organization how this new feature may benefit you. Improving readability, maintainability, robustness, and performance of the function, the module, even the entire code base.

Especially once you start writing automated tests, this becomes a valuable way to learn how use these concepts. When you are still becoming familiar with how to leverage a new feature in realistically complex code, simply attempting to apply it in a well-understood program allows you to quickly learn things about the feature you cannot get from writing a new program.

Get mentoring and guidance. To deeply master Python takes years on your own. Over a decade, in many cases.

To get there faster, apply to work with us. For those who qualify and are dedicated, our mentoring program is consistently proven to accelerate you to the top 1% skill level of Python programmers. And typically does so in 90 days, saving you years or longer. If that sounds good to you, fill out the application here:

Regardless, on to the next chapter.

Next Chapter: Scaling With Generators