Jump to ratings and reviews
Rate this book

Python Cookbook

Rate this book
If you need help writing programs in Python 3, or want to update older Python 2 code, this book is just the ticket. Packed with practical recipes written and tested with Python 3.3, this unique cookbook is for experienced Python programmers who want to focus on modern tools and idioms.

Inside, you’ll find complete recipes for more than a dozen topics, covering the core Python language as well as tasks common to a wide variety of application domains. Each recipe contains code samples you can use in your projects right away, along with a discussion about how and why the solution works.

Topics include:

Data Structures and Algorithms
Strings and Text
Numbers, Dates, and Times
Iterators and Generators
Files and I/O
Data Encoding and Processing
Classes and Objects
Modules and Packages
Network and Web Programming
Utility Scripting and System Administration
Testing, Debugging, and Exceptions
C Extensions

706 pages, Paperback

First published July 15, 2002

Loading interface...
Loading interface...

About the author

David Beazley

10 books9 followers

Ratings & Reviews

What do you think?
Rate this book

Friends & Following

Create a free account to discover what your friends think of this book!

Community Reviews

5 stars
407 (42%)
4 stars
370 (38%)
3 stars
153 (15%)
2 stars
27 (2%)
1 star
12 (1%)
Displaying 1 - 30 of 47 reviews
Profile Image for Sean.
60 reviews13 followers
December 27, 2014
Lots of practical recipes for using Python more effectively.

Just to clarify for certain of my friends (you know who you are!):
* Python is a popular computer language, named after "Monty Python's Flying Circus", a British TV comedy
* The O'Reilly Cookbook series uses the metaphor of cooking to present a collection of small, practical solutions ("recipes") to specific programming problems.

Hence, despite my misleading lead-in, the Python Cookbook has nothing to do with preparing snakes for human consumption.
May 8, 2018
As a Java programmer that wants to learn Python this book was OK.
There are some good recipes but most of the book (about 85%) can be summarized in one sentence "You want to do some job, find third party library to do it for you"
Especially annoying that in some recipes the section ends with something like "there are some better libraries to do this job so use them and not the suggested in recipe?!?!?!"
Some of the recipes are very basic and can be summarized in few lines but in many cases those recipes are stretched to a few pages.
Profile Image for Louis.
219 reviews26 followers
July 14, 2013
The third edition of Python Cookbook is part of what seems to be a general trend of issuing new editions focusing on Python 3. For me, whose focus is on data analysis and technical computing, this is the time to be thinking about the change from Python 2 to Python 3 as the base libraries of Numpy, Scipy, Matplotlib, and iPython have been ported, and the various other libraries I use that depend on these are being ported as well. But this edition is not just porting the old cookbook, it is a complete rewrite to go with the big shift that was Python 2 to 3. Because a lot of what I used the old cookbook and many of the recipes at the ActiveState website was for handling issues related to crossing versions (I had some projects with Jython, which was several versions behind CPython) and ways of getting around issues that are purportedly solved in Python 3. For that the third edition of Python Cookbook fills its purpose of showing idiomatic ways of performing some programming tasks, and being a reference for how to do thing well and elegantly taking advantage of the language and libraries, not fighting it.

I focus on using Python as a data analysis platform. So I generally only learned as much of the language I needed to in order to use the scientific stack of Numpy, Scipy, Matplotlib, Pandas, and the libraries that were built around them. But that means that I have not gotten to know large portions of the standard library. And introductory books don't cover this either, they focus on using the language itself. There are also a large number of books and references that focus on Python as a web development tool or a system administration tool, so those parts of the standard library get a lot of coverage in teaching materials. But the rest you almost have to stumble upon. In ideal conditions, the way you would learn about much of the standard library is to have someone who was more experienced nearby show you what you needed to know, as she demonstrated methods in her code that did the things that you never learned in class. But sometimes there is no such person. The Python Cookbook plays this role, of demonstrating how to do things in Python 3 that are practical and you probably would not learn while learning the language itself.

Some areas that I found useful are the heapq, generators, and the I/O. heapq is a module and data structure I just never got around to learning. Usually discussions about Python data structures made their way to deque and heapq was discussed by reference. But after looking at the priority queue discussion, I fired up an iPython notebook and went through every recipe that used heapq and I've started thinking about how to rewrite a model I recently coded up. Generators and File I/O are areas that I knew in passing through my use with them in data analysis, but Python Cookbook opened up new ways of understanding (I am starting to get why JSON is so useful). Now, there is nothing special about these, but seeing these parts of the standard library in use in an elegant way is something beyond what you would get from a module documentation or a standard tutorial.

I do miss the introductions to each chapter that was in the 1st and 2nd editions of Python Cookbook. But what Python Cookbook does give is an idiomatic feel of using Python3, when there are not all that many mentors out there to go around. So this is something very useful for others who are starting to use Python 3. It is not for learning the language, but it is for using the language well.

Disclaimer: I received a free electronic copy of this book as part of the Oreilly Bloggers program
Profile Image for Jeanne Boyarsky.
Author 30 books65 followers
June 17, 2013
The “Python Cookbook” (3rd edition) is over 600 pages full of content. The recipes vary extensively in difficulty and scope. From simple string concatenation to writing a (BNF) recursive decent parsers.

The book covers Python 3.3. The authors warn some recipes aren't backward compatible with 2.X. While I would have liked more on the transition, I'm not a Python developer – I just do some scripts. It's entirely possible nobody cares about compatibility.

My favorite recipe was on CSV. Paring was shown but a library was also covered along with the reasons to not implement it yourself.

The recipes assume a working knowledge of Python. I have just enough knowledge to be able to follow them. And the book is a great resource with common idioms and techniques. It is certainly an advanced book and I highly recommend it. (If you are starting out with Python, I recommend reading “Think Python” first.)

Disclosure: I received a free copy of this book from the publisher in exchange for writing a review.
Profile Image for gully.
5 reviews2 followers
November 22, 2016
I really liked this book, I learned a lot of Python 3 without even realizing it. When I sat down for a Python 3 project, the new stuff was all second-nature. It covers tons of disparate applications, some of which were over my head. It's a great reference.
Profile Image for Allele.
10 reviews
December 24, 2013
This is idiomatic Python. This book is made all the more enticing by the fact that it focuses on using Python 3. At the time of its publication, it was the best resource for learning how to write lean code using the features available in Python 3. At the time of my writing this review, it is still *the* book I'd recommend for that purpose.

Go get it, and read it twice. Your Python will thank you. :)
7 reviews
August 7, 2017
Great book! A must-have on work desk.

It is so well structured: chapters grouped by programming techniques or domain tasks and each chapter has recipes ordered by complexity level. This makes the book good even for not-so-begginer Python programmers, they can just read first 5 recipes in each chapter or read chapter until they start having problems with smooth reading and understanding.
The recipes include only starndard library ingredients however alternative 3rd party tools are also mentioned. There are few internal recipe cross references in more complex recipes.

For sure I will use this book again, for instance as starting point for C extensions.
Profile Image for Mark Eccles.
1 review
August 9, 2020
I am a part time Python programmer, using it for simple uses cases such as log file parsing, serving RESTful APIs, and machine learning pipelines. This book was an interesting read. I picked up a lot of new knowledge of some of Python's popular modules and language features. Most interesting was the sections on iterators, generators, context managers, and classes. These are done slightly different in Python compared with other languages like Java, so very helpful for someone experience with programming in other languages. It would save me falling into traps or writing unnecessarily long code.

This book also serves as a reference / style guide.
Profile Image for Ethan Swan.
36 reviews
May 9, 2021
Dense and dry, but good. Probably not meant to be read cover-to-cover but that didn’t stop me.

Unfortunately the book is written for a fairly old version of Python (3.3), though most of it remained pretty applicable — the exception was a section on manipulating file names that seems now obsolete with the addition of Pathlib.

Reading this definitely forced me to think more about a lot of the tougher parts of Python that I usually avoid. A worthwhile read for people trying to build a deep expertise in the language.
210 reviews1 follower
November 7, 2018
Szukałem książki z modułami Pythona, a ta jest chyba lepsza, bo jest wszystko co trzeba. O dziwo bardzo dużo przykładów i bardzo przydatnych. Jeżeli przeczytasz to zobaczysz, że dużo rzeczy np. z datami można zrobić błyskawicznie, zamiast bawić się ręcznie. Prawdopodobnie będzie działać szybciej. O dużej ilości funkcji nie słyszałem wcześniej. Autor ma duże doświadczenie i widać dużo czasu spędził z Pythonem pisząc programy na różne sposoby.
6 reviews1 follower
November 28, 2018
It is a good reference book. I read half of the book chapter after chapter and then I pick what was interesting. It is a book to come back. I would prefer if the book had some basic introduction to Python for beginners. I have programming background in C++, Java, PHP, Scala, but haven't had that, it would be way too difficult to go thought. There are many topics and terms that are not explained: closures, packages, lambda, etc. so some basics are required.
Profile Image for bimri.
Author 1 book4 followers
March 12, 2021
I'll call it the no nonsense straight to the point python book - I have come across! The most fast paced python book. Maybe it’s my prior knowledge on the topics of this book; that's why I enjoyed reading it like crazy.

The recipes present a unique manner to understand programming problems in python - and the solutions strengthens one's intuitions on the subject matters! I really loved working the code from this book, such a masterpiece.
Profile Image for Brian Connell.
16 reviews
May 27, 2021
Excellent technical writing IMO. Concise clear writing, with rationale and alternatives for recipes described. Well organized. Written in 2013, I am guessing it may be a bit dated for some solutions/recipes given for advanced topics. I still learned a lot reading through this. Unfortunately the authors twitter feed (2021) says he has just decided not to complete/write an updated version (although he offered the job up to someone else).
44 reviews6 followers
August 7, 2017
This is a great tour through the "fancier" tools available in Python.

(What are generators and iterators and why would you use them? Are there elegant built-in tricks for string parsing and data structures? etc.)

It's structured as "problem: solution!" and is very dense but clear. Works either for reading cover-to-cover and using as a reference.
164 reviews
July 29, 2018
Very useful. An excellent reference. Essential even.

I also recommend reading it cover to cover. That is tough going (it’s an all-encompassing tome) and will take some time. However it’s incredibly valuable for helping shrink the space of unknown unknown best practices in Python. “Oh wait, isn’t there a better way of loading package data files... yes, that’s right...”
Profile Image for Indrani.
44 reviews
December 1, 2018
Excellent material for acing for python skill. This is not for beginners though. You need to have some hands on experience in python to grasp the material. Different recipes to help you understand the advanced concept of the language. This book will make your day job easier. Highly recommended for anyone want to perfect python skill.
Profile Image for Nickolai.
661 reviews7 followers
November 15, 2017
Отличная книга. Могу поставить ее в один ряд с работами Лутца. Все очень понятно, доступно и подается в хорошей форме. Множество интересных примеров. Следует учитывать, что книга рассчитана на уровень выше среднего, а вторая половина - даже на сильно продвинутый. Новичкам покажется дебрями.
8 reviews
April 3, 2019
Excellent compendium of Python recipes and idioms. Particularly useful for experienced programmers who want to become proficient quickly with Python but avoid importing assumptions from other languages.
Profile Image for Kyle.
13 reviews
June 21, 2021
Very good to use as a reference and for ideas on basic algorithms for common Python task. Requires at least an intermediate level of Python skill so not recommended for those learning Python but great for refreshers especially after using Java or C++ for a whle.
Profile Image for Kalyan Tirunahari.
104 reviews1 follower
March 7, 2020
If you are at an intermediate level to Python and want take it to advanced level this is the right book for you. Tons of relevant examples.
Profile Image for Poki.
84 reviews1 follower
January 16, 2023
instructions unclear, i burned my house down trying to cook a snake
Profile Image for Daniel.
156 reviews
November 27, 2010
I'm glad to be done with this for now, as I am with most books intended for reference use that I try reading cover to cover. Nevertheless it's a great resource, and I think the recipes here are a bit higher quality than some of the other O'Reilly Cookbook titles. Good cross-referencing and encouragement to refer to material in related chapters. An essential reference if you are learning Python, and a source for some great ideas.
Profile Image for Dave Peticolas.
1,376 reviews38 followers
October 8, 2014

This Python programming cookbook lives up to the standard set by the excellent Perl Cookbook. In addition, this book is associated with a website where the programming recipes were originally collected from Python users. The website continues to grow and collect new recipes and thus represents a great resource for Python programmers.

Profile Image for Eric Casteleijn.
11 reviews4 followers
December 10, 2015
A solid Python reference, containing recipes for many common tasks. A valuable addition to any Pyhonista's library.

If I had to find fault with anything it's that the code samples could use a little cleaning up. The coding style is inconsistent (understandably with two authors), and not always in agreement with generally accepted best practices (pep8). I feel running the examples through a linter would have fixed that, but it's immaterial to the utility of individual examples.
Profile Image for idle sign.
36 reviews1 follower
September 28, 2014
Внутри ровно то, что обещали в аннотации: поваренная книга с рецептами применения встроенных и сторонних библиотек. Базовые знания о Питоне, прежде чем начать читать обязательны. Про примеры, характерные для Python 3 тоже не соврали — их довольно много. Рекомендую, наверняка найдёте для себя что-нибудь интересное.
Displaying 1 - 30 of 47 reviews

Can't find what you're looking for?

Get help and learn more about the design.