The Python 2 Standard Library by Example

Distributed with every copy of Python, the Standard Library contains hundreds of modules that provide tools for interacting with the operating system, interpreter, and Internet—all of them tested and ready to be used to jump-start the development of your applications. This book presents selected examples demonstrating how to use the most commonly used features of the modules that give Python its “batteries included” slogan, taken from the popular Python Module of the Week (PyMOTW) blog series.

Preview Online

Table of Contents

The Table of Contents for the book lists all of the modules covered.

Sample Chapters

A free PDF containing the table of contents, foreword, introduction, index, and chapter 2 (“Data Structures”) from the final version of the manuscript is available on publisher’s web site.

A free e-book containing the table of contents, foreword, introduction, index, and chapter 1 (“Text”) is available through the iTunes book store.

Safari Books Online

The full text of the book is available through Safari Books Online. This digital edition matches the print and other digital versions completely.

Read What Reviewers Have To Say


…I like this class of documentation that is between a cookbook and a reference manual. I find it useful that the examples are not so terse nor overly verbose. I also appreciate the quality of the code and the references for further readings. I think that this book fills a void that will make many Python programmers more proficient.

—Ahmed Al-Saadi

Python Zone

This book is and will be a key resource for many serious Python programmers and provides a resource for understanding and using many of the Standard Library capabilities. It will be in a handy space on my bookshelf and is highly recommended.

—Tom Brander

Musings of an Anonymous Geek

… the book is written by an authoritative, knowledgeable, experienced, and prolific Python developer. While he’s a creative thinker, his work is balanced by a healthy dose of pragmatism and grounded in best practices.

—Brian Jones

Triumph Over Madness

My copy of The Python Standard Library by Example arrived this morning and it’s totally kick-ass.

—Reed Wade

Order Now!

Order your copy today by going directly from the publisher’s web site or from one of these online booksellers:

This Book’s Target Audience

The audience for this book is an intermediate Python programmer, so although all of the source code is presented with discussion, only a few cases include line-by-line explanations. Every section focuses on the features of the modules, illustrated by the source code and output from fully independent example programs. Each feature is presented as concisely as possible, so the reader can focus on the module or function being demonstrated without being distracted by the supporting code.

An experienced programmer familiar with other languages may be able to learn Python from this book, but it is not intended to be an introduction to the language. Some prior experience writing Python programs will be useful when studying the examples.

Several sections, such as the description of network programming with sockets or hmac encryption, require domain-specific knowledge. The basic information needed to explain the examples is included here, but the range of topics covered by the modules in the standard library makes it impossible to cover every topic comprehensively in a single volume. The discussion of each module is followed by a list of suggested sources for more information and further reading, including online resources, RFC standards documents, and related books.

Although the current transition to Python 3 is well under way, Python 2 is still likely to be the primary version of Python used in production environments for years to come because of the large amount of legacy Python 2 source code available and the slow transition rate to Python 3. All of the source code for the examples has been updated from the original online versions and tested with Python 2.7, the final release of the 2.x series.

Downloading the Example Code

All of the source code for the examples in the book can be downloaded and used under the BSD license.

The original versions of the articles are available on the PyMOTW site.


Be the first to report an issue via the bug tracker on the issue tracker. As updates are made based on errata, they will be posted to