The more popular or more powerful a programming language, the better the odds of finding not only many books about it, but a diversity of books. As Python has soared in popularity, so have the number and variety of the books created to help people learn the language and master its intricacies.
Here are seven of the best books on programming with Python, ranging from beginner’s guides to power-Python proficiency. Whether you’re just starting out, or you’ve been working with Python for some time, there is most likely a book here for you. A few are available in online or PDF editions for free.
Automate the Boring Stuff with Python
Most introductions to programming languages, or to programming in general, are conceptual. They talk about programming in the abstract. Rob Sweigart’s Automate the Boring Stuff with Python talks about programming in the particular: It uses a course in Python as a way to learn how to turn tedious tasks into snappy Python scripts.
Automate is pitched at beginners to Python and computing generally. It opens by getting the reader comfortable with a simple IDE (the “Mu” editor), then steps them through the use of input, flow control, functions, object collections (lists, dictionaries), validating input and pattern matching, and working with files.
The second half of the book is more task- and project-oriented. It covers common automation tasks: working with spreadsheets and text documents, sending emails, manipulating images, and automating GUI interactions.
What makes Automate a valuable beginner’s book is how it rolls in, all along the way, projects that are suited to the current level of lesson — such as a simple (albeit verbose) rock-paper-scissors game to teach flow control, or a Pig Latin generator to teach string manipulation. It also takes the time to map out, again and again, how to translate what we want to do (first this, then this, then this) into what the program does.
A Byte of Python
“Unpretentious” is the best word to describe Swaroop Chitlur’s A Byte of Python. It’s easily the most straightforward and accessible guide to Python around, if also one of the least adorned. It doesn’t use gimmicks, projects, or cute illustrations, but omitting them is also among its biggest strengths: nothing comes between the reader and the lessons.
The book guides the reader first through setup and configuration of a Python instance, then the use of the REPL and an editor (PyCharm gets most of the coverage there). From there it steps through variables and types, operators, control flow, functions, modules, data structures (including classes, although there’s a full chapter on OOP), I/O, exceptions, and quick overviews of notions like list comprehensions and decorators.
The book has a few slight drawbacks. One is organization: Input and output are handled quite late, when most tutorials teach it much earlier. Also, some items that deserve to be in a basic Python overview, like context managers, aren’t covered at all. But as a whole, the book is a good no-frills introduction to the language.
Amazon (Kindle only): https://www.amazon.com/Byte-Python-Swaroop-C-H-ebook/dp/B00FJ7S2JU
Learning Python, 5th Edition
Programming Python, 4th Edition
For sheer comprehensiveness and all-in-one-ness, nothing beats Mark Lutz’s Learning Python and Programming Python. Both books are over sixteen hundred pages each, but don’t let the size scare you off — they’re meant to be digested topically, rather than read end-to-end.
Just about every topic in Python, as of Python 3.3, is covered in Learning Python, and covered in exhaustive depth. If you want to learn everything there is to know about decorators, for example, the chapter on that topic constitutes a mini-course on the subject.
Programming Python is about using Python to build real-world applications. Rather than just illustrating the language’s components, it explores system programming, GUIs, Internet clients and servers, databases, integration with C, and much more.
The biggest downside of Learning Python isn’t its size, but its age. The 5th edition came out in 2013, covering both Python 3.3 and 2.7, so doesn’t cover many of the significant innovations rolled into Python since then. As of 2016, Lutz had no plans to update it, in part due to his documented difficulties with the publisher.
High Performance Python: Practical Performant Programming for Humans
Anyone remotely interested in making Python faster or more efficient should add this book to their collection.
“Python” and “high performance” are not often spoken in the same breath. What Python gives you in convenience, it takes away in raw, machine-level speed. But that doesn’t mean high-speed Python is impossible; like many “impossible” things, it just takes a little more work.
High Performance Python: Practical Performant Programming for Humans, by Micha Gorelick and Ian Ozsvald, walks experienced Python programmers through all of the ways Python code can be made faster, from simple pure-Python optimizations to rolling custom C code. The book begins with a dive into application profiling, a key skill for diagnosing and correcting performance issues with any Python app, then explores specific optimizations in turn:
- Access patterns and big-O performance of various data structures, like lists versus dictionaries and sets.
- How generators can be used to save memory for large computational problems.
- Using matrices and vectors — essentially, an introduction to using NumPy and Pandas for faster math.
- Using Cython, Numba, PyPy, and other third-party compilers and runtimes for machine-level speed. A discussion of each of those could occupy an entire book (in Cython’s case, it does) of its own, so only the most basic and common use cases are covered here. But you’ll be well-prepared for further learning.
- Using asynchronous programming to speed up multiple I/O-dependent operations.
- Using multiprocessing to avoid the GIL, and using clustering and job queues to further divide work.
A lengthy chapter is also taken up with notes on solutions to real-world Python performance issues, including deploying machine learning pipelines. The second edition of the book updates the text for the latest versions of Python, and includes material on working with GPUs.
Once you’ve mastered the basics, what next? Fluent Python answers that question.
Programmers who already know enough Python to write their own programs often struggle getting to the next stage: making use of Python’s unique featureset to write truly powerful software. Fluent Python, by Luciano Ramalho, walks the programmer through many key aspects of Python as leveraged by experts: mastering the Python data model and “dunder methods,” advanced use of data collections like sets and dictionaries, creating objects that work as records (not just classes, but named tuples and dataclasses), using functions as objects, type hinting, and much more.
While some of the material (e.g., lambdas) may not be new to even moderately experienced Python programmers, the book shows how these elements are valuable to advanced programmers. Even if you’ve used these Python features before, the book will show you how to build more robust programs with them, and how to choose wisely between similar alternatives (e.g., structs and memoryviews) for advanced work.
The subtitle of this book is “How To Think Like A Computer Scientist,” which gives you a hint of the book’s intentions. Think Python, by Allen B. Downey, is a guide to learning Python aimed at total beginners, but its larger goal is to help the reader understand what programming is about, what it means to be a programmer, and why computer programs work the way they do. Python is just the arena in which those ideas are explored. Given how easy Python is to learn, it’s a sensible choice.
Think Python begins with formal concepts — what is a program, what are input and output, what are types and values, and how programs deal with information in those terms. From there the book moves on to how statements and orders of operation work, and how statements can be strung together into expressions and functions. Condition handling and control flow, iteration, collection types (strings, lists, dictionaries), file I/O, classes, and inheritance, along with a slew of useful Python features tagged “Goodies,” all receive chapters of their own.
What’s most appealing about Think Python, apart from its clear and direct language, is how it constantly relates each bit of information to the central themes of how computers and computer programs work, and which techniques are used in programming to achieve what ends. For newcomers, this is more important than they might at first realize.
Think Python is available as a free ebook in PDF or HTML format.