The Python 3 Standard Library by Example

cover image for The Python 3 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

Coming Soon!

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.

Order Now!

Order your copy today 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.

Python 3 versus 2

The Python community is currently undergoing a transition from Python version 2 to Python version 3. As the major version number change implies, there are many incompatibilities between Python 2 and 3, and not just in the language. Quite a few of the standard library modules have been renamed or otherwise reorganized in the new version.

The Python development community recognized that those incompatibilities would require an extended transition period, while the ecosystem of Python libraries and tools was updated to work with Python 3. Although many projects still rely on Python 2, it is only receiving security updates and is scheduled to be completely deprecated by 2020. All new feature work is happening in the Python 3 releases.

It can be challenging, though not impossible, to write programs that work with both versions. Doing so frequently requires examining the version of Python under which a program is running and using different module names for imports or different arguments to classes or functions. There are tools, outside of the standard library, to make the process simpler. To keep the examples in this book as concise as possible, while still relying only on the standard library, they are focused on Python 3. All of the examples have been tested under Python 3.5, the current release of the 3.x series at the time they were written, and may not work with Python 2 without modification. For examples designed to work with Python 2, refer to the Python 2 edition of the book, called The Python Standard Library By Example.

Again, in an effort to maintain clear and concise descriptions for each example, the differences between Python 2 and 3 are not highlighted in each chapter. The Porting Notes appendix summarizes some of the biggest differences, and is organized to be useful as an aid when porting from Python 2 to 3.

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