Hacker News new | past | comments | ask | show | jobs | submit login
Ask HN: ______ is to Python as K&R is to C?
42 points by HedgeMage on Apr 30, 2011 | hide | past | favorite | 50 comments
I'm in need of a book recommendation for learning Python (not learning programming, just adding this language to my repertoire).

My learning method is more "start coding something, and refer to a reference book frequently" than "do these lessons and you'll eventually learn enough to start coding something you actually care about". A good reference is dense and to the point; it makes it quick and easy to look up both the syntax of individual features, and the thinking behind them.

Basically, my ideal programming book is Kernighan and Ritchie's "The C Programming Language" -- I want that, but for Python.

All the books recommended so far are good books, but none of them really answer the author's question. In particular, Learn Python the Hard Way is pretty much the exact opposite of what he is looking for.

The closest thing to K&R C for python is Python Essential Reference by Beazley. The structure mirrors K&C. The first chapter is a "tutorial introduction", basically a quick survey of the language, followed by about 150 page explaining the language. It is dense and terse and to the point. The remaining 400 pages are a reference for the python library.

Why, in 2011, are people still selling books with hundreds of pages of reference material in them? Has anyone needed or wanted to consult a printed reference on the Python standard library in the last decade?

I got Ruby Best Practices and the Pragmatic Bookshelf's 'Programming Ruby 1.9' from the library last summer, and had an incredibly productive time. I would sit in front of my laptop reading RBP, and any time I was confused by a class or anything, flip there in the reference and see what the method did. I could keep my terminal up on screen and not switch into "web browser + search engine" mode at all.

Having a book is like having an additional monitor full of information that is uncompromising on your task. It's also wonderful to read, and easy to pop open even when your laptop isn't out, and you're just curious "I wonder if Array has a method to do ...."

A final note, because of that, having a paper reference (especially an annotated one) lends itself strongly to actually reading large swaths of it, instead of just finding the method you need and putting it away. With it being clunky, you don't want to do a lookup twice, so you open it and keep it open, and often read through a module/class' entire method list. That really improves your knowledge.

So in short, because it encourages good work and work practices.

Obviously, I want one, or I wouldn't have asked!

The back of the book is not just a copy and pasted version of the standard python docs, it is more of an annotated reference. People pay money for this stuff because they either want a different viewpoint or more indepth coverage on certain topics.

I could write a book just answering this question. I find that most online reference sites, while extremely useful, lack a certain style, as well as, you know, in-depth treatment of language concepts.

It is also harder to flip back and forth between 2 or more screens to check related details than it is between multiple pages. Unless you are just checking a fairly specific, narrow detail, books are just more efficient in time and effort.

Thanks, angrycoder, this sounds like the closest thing to what I'm looking for.

> The closest thing to K&R C for python is Python Essential Reference by Beazley. The structure mirrors K&C.

This is probably pretty intentional. I had Dave as a professor for several C based classes and he recommended K&R highly. In the introductory course, he even used it as the textbook.

(btw I also have attended one of his new Chicago Python classes - recommended, he's a great teacher)

The official Python, http://docs.python.org/tutorial/, written by Guido van Rossum himself, is one of the most concise introductions to Python I've read. Just skip the first few chapters.

Other good books are:

* Learn Python the Hard Way -- http://learnpythonthehardway.org/index

* How to Think Like a Computer Scientist/Think Python (now Python for Software Development) -- http://www.greenteapress.com/thinkpython/

Dive Into Python is good, but IIRC (from Dive Into Python 2), Mark Pilgrim tends to use very convoluted solutions to simple problems. The solution might be superior in terms of performance, but don't always help you learn the language.

If you know how to program allready I really don't recommend 'Learn Python The Hardway'... it might bore you.

Like K&R, the official docs were written by the language designers/implementors. I think that's important: it eliminates a level of misunderstanding (between the designers and the writers).

I end up going back to "Dive Into Python" for reference. http://diveintopython.org/

My other two references are the Python Standard Library Docs (http://docs.python.org/library/index.html) and StackOverflow.

Dive Into Python would be perfect for the OP's requirements. I touches on many aspects of python with code samples and is done in a concise way.

Zed Shaw disagrees: http://oppugn.us/posts/1272050135.html , and I think he has a point.

"The book is weird, uses antiquated technology, has horrible examples [...]"

Dive into Python has code examples which use ODBC:

"He has a [...] book that purports to teach people programming that still, in 2010, references a piece of crap technology from the 90's. Yes, he actually used that technology to teach Python in the beginning of his book six years ago. [...]

Meanwhile, Mr. Pilgrim's book hasn't been updated in 6 years even though it's [...] online and people can send him patches. [...]

Dive Into Python isn't just bad because of the use of ODBC, it's also just full of bad initial examples. Take a look at your first Python program and boggle at all the bizarre stuff a beginner has to suddenly comprehend:

- A function, with a giant doc comment right away.

- Weird underscore variables with a bad font making it look like one underscore.

- A list comprehension for-loop to join a string using a format string off a tuple.

- A dict, formatted with backslash chars that aren't even needed.

Holy crap, how in the hell is that a good first example? Even worse is it starts a trend within the book of using ODBC as a theme to teach Python."

Edit: Snipped strong language.

I disagree with Zed Shaw here. Ignoring the ODBC problem, Dive into Python is written well for its target audience. This book is written for people who already know how to program. Seeing a function with a doctstring, list comprehensions, etc all at once is exactly the way I wanted to get a feel for what python was about. It served its purpose very well. Now I will admit, some of the examples did get tedious to the point where I skimmed them. But overall the book served its purpose well.

Many of these are tutorials. You've asked for more of a reference. I've heard people say that Beazley's Python Essential Reference is as close to a Python K&R as there is, but I'm not experienced enough in Python to know.

Yes. Beazley's essential reference is the way to go. The docs at the Python website are still essential for some things, though, since newer 2.X versions of Python often have minor changes/additions from previous versions, such as library changes and backports from Python 3.

Yes, Python Essential Reference by David M. Beazley is great. If you already know how to program, this is the one to buy and keep on your desk.

I agree. I think it's a great choice for experienced programmers: concise and to the point and exhaustive.

Python doesn't have one. It is too dynamic (especially compared to C), adding new features year after year.

I'd say use the official docs combined with a cookbook or "learning python" book (which is pretty big).

K&R got to be K&R because C really didn't change that much.

Upvoted for the insight on why I'm having trouble finding a K&R equivalent for Python.

I bought "Learning Python" because it covers both 2.x and 3.x, but found it absolutely unnavigable as a reference. It might work well for people who want a tutorial they can go through step by step, but for someone who just wants to quickly find and absorb info in the process of doing something else, it doesn't cut the mustard.

I disagree, for instance, here's a (IMO) exact description of the K&R 'relevance':

"It's not even a very good way to learn C. The idioms and best-practices for C programming have evolved substantially, even since the second edition was published, and some of the code samples look a bit dated." And this is from 2004, meaning it got even more dated.(http://sites.google.com/site/steveyegge2/ten-great-books)

The reason K&R is still the canonical C book is because it represents the language better than any other book, before or since, not because the language stagnated since ANSI C was created.

K&R is the book people recommend because it WAS a great book for like 12 years. It just hasn't been since about 2002.

That's true, but, IMO, there's a difference between learning to program in a language and learning a language (aka "Thinking in X"). The K&R is still the best source for the latter.

I find that K&R uses some really stupid functions to do things, and as a result causes many OTHER people do do some stupid things to do things.

I find that makes it wholly in adequate for thinking in C anymore. The "Never trust data" mantra which is now part and parcel of C dev is not at all shown in K&R.

This is exactly what I meant: I could make due with what I've already found for learning the syntax of Python -- but I haven't yet found the one that teaches me to think in Python.

I am surprised that O'Reilly's "Learning Python" hasn't received more nods. Granted it's long (1200 pages as I recall), but it takes the reader on the full journey through all of Python's multitude of features in a very structured way, with each chapter building on the foundations the other chapters lay.

I recommend "Dive Into Python" to people who want to start to get a taste of Python, but anybody that has developed a appetite for Python and wants to really, really Grok how Python works, I recommend O'Reilly's "Learning Python" and tell them to take the time to read it cover-to-cover.

I recommend The Python Cookbook: http://oreilly.com/catalog/9780596001674. It's not a reference in the sense that it shows you the language syntax or tells you the details of the standard library, but it shows how experienced Pythonistas do things in a way that might not be obvious to you if you come from a language like C or Java, or until you're Dutch.

Python in a Nutshell, O'Reilly Media, written by Alex Martelli http://oreilly.com/catalog/9780596100469/

Definitely recommended for learning Python fast&practically. It's the most comprehensive and useful Python reference book for programmers I've ever encountered.

Thanks for the recommendation, serialx; this (along with Essential Reference) is contending to be my next purchase. :)

I see, in this thread, a lot of good examples wrt good python books, but the nature of the question left me to wonder. When I hear "What is the K&R of X", it makes me think of "what is the book, preferably by the creator of the language, that represent, most faithfully, the spirit of the language".

For example, compare Programming Perl (aka the Camel book) to K&R, the former is a long, didactic, journey through Perl's linguistic building blocks, while the latter is a laconic, technical, concise guide to C. I think these also embody the philosophy and nature of these languages. Another example would be Lisp/Scheme books that tend to be academic, and theoretical.

The official docs are great, but I personally find them difficult to navigate.

I have found that Googling for "python docs X" usually has the python doc page for X as the top hit.

Here is my list of recommended online Python tutorials, in order from introductory to more advanced (http://www.quora.com/How-can-I-learn-to-program-in-Python/an...). It also includes my dead-trees recommendation -- "Python Cookbook," by Alex Martelli.

The Python overview in the Web2py docs might be sufficient (http://web2py.com/book/default/chapter/02). It's very concise but covers the basics. Then, for reference, the Python docs are the way to go.

I feel horrible for derailing, but can I ask HN for a similar book for Ruby? I know the basics of RoR, but feel like I would be a lot better if I knew more about the Ruby underpinnings.

The Ruby Programming Language by David Flanagan and Yukihiro Matsumoto (the creator of Ruby) is very good.

The coverage, speed and size (half the size of Pickaxe book) make you productive quickly.

...and it's intentionally structured in a way that mirrors K&R!

I've read many Ruby books over 6 years. The best, in my opinion, is David Black's "The Well-Grounded Rubyist". I read it just last year as an experienced Rubyist. I thought it had a fantastic, no-magic take on the language that offered a better perspective on scope, metaprogramming, and general language design than any other book I've read. That said, I haven't read the Matz book.

Programming Ruby (aka "the Pickaxe").

I never fully grokked Ruby's object model until I read that book.

I reread it often and always discover/re-discover useful information.

"Python Pocket Reference, Fourth Edition" (http://oreilly.com/catalog/9780596158095/)

I used the Quick Python Book (Manning), which was perfect coming from Ruby. If you're coming from anoth dynamic scripting language, I highly recommend it.

Python Cookbook is the best if you do not need to learn programming.


If you were to find a K&R for Python, then it would be missing an important something which is unique to Python - an acknowledgement and appreciation of absurdity.

[edit: was "good humor"].

The Quick Python Book - Daryl D Harms Ph.D.

I like Learn Python The Hard Way by Zed Shaw: http://learnpythonthehardway.org/



That page says "If you can already code then the book will probably drive you insane", which doesn't match the poster's request.

(The parent, now deleted, was referring to http://learnpythonthehardway.org/ which a couple of other posters also link to.)

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact