News for hackers. Stuff that matters.

Ask HN: ______ is to Python as K&R is to C?

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.

( 50 comments | 42 points | on HN )
Re:Ask HN: ______ is to Python as K&R is to C? by angrycoder on Saturday April 30, 2011 @11:02AM (#2500746)
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.

Re:Ask HN: ______ is to Python as K&R is to C? by thurn on Saturday April 30, 2011 @11:07AM (#2500758)
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?
Re:Ask HN: ______ is to Python as K&R is to C? by wcarss on Saturday April 30, 2011 @12:22PM (#2500913)
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.

Re:Ask HN: ______ is to Python as K&R is to C? by HedgeMage on Saturday April 30, 2011 @11:32AM (#2500810)
Obviously, I want one, or I wouldn't have asked!
Re:Ask HN: ______ is to Python as K&R is to C? by angrycoder on Saturday April 30, 2011 @12:03PM (#2500873)
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.
Re:Ask HN: ______ is to Python as K&R is to C? by dwolfson20 on Saturday April 30, 2011 @12:23PM (#2500915)
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.
Re:Ask HN: ______ is to Python as K&R is to C? by billswift on Saturday April 30, 2011 @01:56PM (#2501117)
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.
Re:Ask HN: ______ is to Python as K&R is to C? by HedgeMage on Saturday April 30, 2011 @11:34AM (#2500813)
Thanks, angrycoder, this sounds like the closest thing to what I'm looking for.
Re:Ask HN: ______ is to Python as K&R is to C? by timf on Saturday April 30, 2011 @02:55PM (#2501249)
> 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)

Re:Ask HN: ______ is to Python as K&R is to C? by thirsteh on Saturday April 30, 2011 @10:22AM (#2500677)
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.

Re:Ask HN: ______ is to Python as K&R is to C? by derrida on Saturday April 30, 2011 @10:55AM (#2500734)
If you know how to program allready I really don't recommend 'Learn Python The Hardway'... it might bore you.
Re:Ask HN: ______ is to Python as K&R is to C? by kbob on Saturday April 30, 2011 @11:52AM (#2500846)
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).
Re:Ask HN: ______ is to Python as K&R is to C? by ScottBev on Saturday April 30, 2011 @10:12AM (#2500652)
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.

Re:Ask HN: ______ is to Python as K&R is to C? by arasraj on Saturday April 30, 2011 @11:54AM (#2500853)
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.
Re:Ask HN: ______ is to Python as K&R is to C? by Luyt on Saturday April 30, 2011 @12:04PM (#2500874)
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.

Re:Ask HN: ______ is to Python as K&R is to C? by hackinthebochs on Saturday April 30, 2011 @01:52PM (#2501107)
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.
Re:Ask HN: ______ is to Python as K&R is to C? by fiore on Saturday April 30, 2011 @10:37AM (#2500700)
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.
Re:Ask HN: ______ is to Python as K&R is to C? by Ratufa on Saturday April 30, 2011 @11:12AM (#2500775)
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.
Re:Ask HN: ______ is to Python as K&R is to C? by gmantastic on Saturday April 30, 2011 @10:55AM (#2500733)
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.
Re:Ask HN: ______ is to Python as K&R is to C? by samuel on Saturday April 30, 2011 @11:07AM (#2500756)
I agree. I think it's a great choice for experienced programmers: concise and to the point and exhaustive.
Re:Ask HN: ______ is to Python as K&R is to C? by gte910h on Saturday April 30, 2011 @11:08AM (#2500768)
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.

Re:Ask HN: ______ is to Python as K&R is to C? by HedgeMage on Saturday April 30, 2011 @11:37AM (#2500821)
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.

Re:Ask HN: ______ is to Python as K&R is to C? by erez on Saturday April 30, 2011 @01:40PM (#2501075)
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.

Re:Ask HN: ______ is to Python as K&R is to C? by gte910h on Saturday April 30, 2011 @03:30PM (#2501352)
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.
Re:Ask HN: ______ is to Python as K&R is to C? by erez on Sunday May 01, 2011 @03:53AM (#2502642)
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.
Re:Ask HN: ______ is to Python as K&R is to C? by gte910h on Wednesday May 04, 2011 @02:34PM (#2514781)
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.

Re:Ask HN: ______ is to Python as K&R is to C? by HedgeMage on Sunday May 01, 2011 @01:01PM (#2503383)
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.
Re:Ask HN: ______ is to Python as K&R is to C? by jfxberns on Saturday April 30, 2011 @11:03PM (#2502193)
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.

Re:Ask HN: ______ is to Python as K&R is to C? by pillmuncher on Saturday April 30, 2011 @10:43AM (#2500714)
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.
Re:Ask HN: ______ is to Python as K&R is to C? by serialx on Saturday April 30, 2011 @11:11AM (#2500773)
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.

Re:Ask HN: ______ is to Python as K&R is to C? by HedgeMage on Saturday April 30, 2011 @11:51AM (#2500845)
Thanks for the recommendation, serialx; this (along with Essential Reference) is contending to be my next purchase. :)
Re:Ask HN: ______ is to Python as K&R is to C? by erez on Saturday April 30, 2011 @01:24PM (#2501030)
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.

Re:Ask HN: ______ is to Python as K&R is to C? by john2x on Saturday April 30, 2011 @10:17AM (#2500660)
The official docs are great, but I personally find them difficult to navigate.
Re:Ask HN: ______ is to Python as K&R is to C? by scott_s on Saturday April 30, 2011 @10:28AM (#2500688)
I have found that Googling for "python docs X" usually has the python doc page for X as the top hit.
Re:Ask HN: ______ is to Python as K&R is to C? by espeed on Saturday April 30, 2011 @11:06AM (#2500754)
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.
Re:Ask HN: ______ is to Python as K&R is to C? by pbreit on Saturday April 30, 2011 @12:46PM (#2500954)
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.
Re:Ask HN: ______ is to Python as K&R is to C? by terinjokes on Saturday April 30, 2011 @10:33AM (#2500696)
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.
Re:Ask HN: ______ is to Python as K&R is to C? by dimmuborgir on Saturday April 30, 2011 @10:53AM (#2500730)
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.

Re:Ask HN: ______ is to Python as K&R is to C? by cannedprimates on Saturday April 30, 2011 @11:39AM (#2500823)
...and it's intentionally structured in a way that mirrors K&R!
Re:Ask HN: ______ is to Python as K&R is to C? by r00k on Saturday April 30, 2011 @10:37AM (#2500701)
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.

Re:Ask HN: ______ is to Python as K&R is to C? by djacobs on Saturday April 30, 2011 @12:18PM (#2500902)
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.
Re:Ask HN: ______ is to Python as K&R is to C? by richardhsu on Saturday April 30, 2011 @11:07AM (#2500761)
"Python Pocket Reference, Fourth Edition" (http://oreilly.com/catalog/9780596158095/)
Re:Ask HN: ______ is to Python as K&R is to C? by djacobs on Saturday April 30, 2011 @12:19PM (#2500908)
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.
Re:Ask HN: ______ is to Python as K&R is to C? by helwr on Saturday April 30, 2011 @11:35AM (#2500817)
Re:Ask HN: ______ is to Python as K&R is to C? by tretiy3 on Saturday April 30, 2011 @12:32PM (#2500930)
Python Cookbook is the best if you do not need to learn programming.
Re:Ask HN: ______ is to Python as K&R is to C? by brudgers on Saturday April 30, 2011 @11:04AM (#2500752)
"Monty"

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"].

Re:Ask HN: ______ is to Python as K&R is to C? by thdn on Saturday April 30, 2011 @09:42PM (#2502074)
The Quick Python Book - Daryl D Harms Ph.D.
Re:Ask HN: ______ is to Python as K&R is to C? by billiob on Saturday April 30, 2011 @10:20AM (#2500674)
I like Learn Python The Hard Way by Zed Shaw: http://learnpythonthehardway.org/
Re:Ask HN: ______ is to Python as K&R is to C? by scscsc on Saturday April 30, 2011 @01:18PM (#2501016)
Nothing.
[deleted]
Re:Ask HN: ______ is to Python as K&R is to C? by imurray on Saturday April 30, 2011 @10:23AM (#2500678)
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.)