Dive Into Python
However, from time to time, you can find a programming language book that stands apart. You can tell from the way the author writes, the topics s/he covers, the unique presentation style and insight that s/he brings that the book is a labor of love. These books enjoy placement on the shelf closest to my desk -- that is, if they're not propped open beside my computer. Dive Into Python is such a book.
One thing that sets Dive Into Python apart from many other programming language books is that its author, Mark Pilgrim, didn't originally plan to make any money from it. As we often say in Open Source circles, he simply had an itch and decided to scratch it. Mark explains this in a story on his weblog in the form of a dialog between him and his manager after showing him a rough 20-page draft:
Manager: "This is really good. You could probably make some money off this someday."
Mark: "Maybe, but I'm not going to. I'm giving it away for free."
Manager: "Why would you do that?"
Mark: "Because this is the way I want the world to work."
Manager: "But the world doesn't work that way."
Mark: "Mine does."
First released in late October 2000 and published in online and downloadable forms under the GNU Free Documentation License, Dive Into Python had grown in fits and starts until 2003, when Mark declared the project closed. Even as an unfinished work, it was held in such high regard by the Python community that developers consistently recommended it; it was also included with ActiveState's Python and FreeBSD's ports distributions. When Mark announced that Apress had decided to pay him to finish the book and publish it, it became the most-anticipated book on Python ever. Even better, Apress has been gracious enough to allow Mark's world to work way it always has: Dive Into Python is still available for free download and is still under the GNU FDL.
What's in Dive Into Python
Many programming language books follow what I like to call the "Computer Science 101 Format", with the first few chapters devoted to covering basic concepts that any moderately experienced programmer already knows. Whenever I leaf through such a book and encounter a chapter that tries to reintroduce me to data types, looping or branching, I feel cheated; I'm essentially paying for a big chunk of book that I'll never read. If you've ever been annoyed by such filler, you'll find Dive Into Python a refreshing change. Rather than wasting time and trees devoting whole chapters to rehashing Computer Science 101, Mark chose to build each chapter after the first around a program that illustrates a number of Python features and programming techniques.
The programs upon which Dive Into Python's chapters are based strike a carefully-maintained balance. They are rich enough to illustrate a number of points and be the basis for some "real world" code, yet small enough to be comprehensible tutorials. For example, chapters 2 and 3 are based on "Your First Python Program", which is a mere six lines of code. However, in those six lines, you are introduced to function declarations, documentation strings, objects and their attributes, importing modules, Python's indentation rules, the "if __name__" idiom, dictionaries, lists, tuples, string formatting and list comprehensions. Within the first hundred pages, a point where many books are re-acquainting you with the "else" keyword, Dive Into Python covers the aforementioned topics as well as Python's reflection capabilities, list filtering, the "and-or trick", lambda functions, OOP and exception handling, all with enough thoroughness to be useful. After reading Dive Into Python, you may have trouble reading other programming language books because they'll seem glacially slow and fluff-laden in comparison.
For the first two-thirds of the book, Mark continues with this approach, presenting a program and then analyzing it to see what makes it tick, teaching Python and oftentimes a programming technique along the way. Each program covers useful tasks that you're likely to run into while programming and does so in an interesting way. At the same time, concepts are introduced in a way that makes sense. For instance, chapter 4 covers two topics that mesh together quite well -- exceptions and file handling -- and it does this by exploring an interesting application: a program that displays the ID3 tag information about each file in your MP3 collection. Later chapters explore regular expressions, HTML and XML processing and Web services. By the time you've finished the first two-thirds of Dive Into Python, you'll have been introduced to enough Python to start writing a wide array of "real world" applications. The book might have benefited from having a chapter covering database access, a task that's at least as common or as useful as accessing Web services, but that's a minor complaint.
While the first two-thirds of the book concerns itself with helping the reader become a Python programmer, the final third is about elevating Python programmers above mere competence. It covers useful topics (albeit rarely-covered in language books) such as refactoring and performance optimization as well as ones that may be new to even some experienced programmers: unit testing, functional programming and dynamic functions. Each chapter in this section is still based on an example program, but rather than analyzing a completed program, its evolution is traced. Although you can get by as a Python programmer without ever reading the material in this section, you'll be a much better one for having done so.
In keeping with the spirit of Python, Mark writes the chapters to present the material as completely and clearly as possible without extra clutter. If there's any additional material that doesn't apply directly to what he's trying to explain, he provides references or links to that material rather than attempting to "fatten up" the book.
The book's long gestation period, assisted by years of reader feedback and James Cox's editing has paid off. It doesn't have the rushed feel that many language-of-the-moment books have (especially the ones written by an army of authors, each one taking a chapter). As far as I know, there isn't any of the sloppiness that pervades many programming books these days, save one instance of the popular typo "teh" (and really, what truly 1337 book doesn't have one of these?).
Mark is aware that Python is likely not to be the reader's first programming language; it's more likely to be some descendant of ALGOL (or more precisely, a language that borrows heavily from either C or BASIC). He also knows that many programmers tend to misapply techniques from the languages with which they're familiar to the language they're learning. With these in mind, he's taken great care to introduce Python idioms as soon as possible. If you follow his advice, you'll be writing "real" Python and taking advantage of what the language has to offer rather than just writing Python-flavored version of whatever programming language you're most comfortable with.
Dive Into Python's Audience
The "user level" specified on the back cover of this book says "Beginner - Intermediate", which I feel is a little misleading. As I mentioned earlier, the book takes great care not to rehash topics with which programmers with some experience are already familiar and is written with the assumption that the reader is proficient in at least one object-oriented programming language. I think many programming novices would be overwhelmed with the speed with which Python features are introduced.
Experienced programmers, whether they are new to Python or are fluent with the language will benefit the most from the book. One programmer I know works with Python daily and and even submitted a patch to wxPython; even he said that Dive Into Python showed him things about Python that he never knew. If you're tired of books aimed at "Introduction to Computer Science" students, you're going to love this book. This doesn't mean that people who don't normally program can't benefit from the book: Joi Ito, who is a tech entrepreneur and not a programmer, learned enough from Dive Into Python to put together jibot, a bot for the IRC channel that bears his name. If you're new to programming, you might want to make Dive Into Python your second book or supplement it with an introductory text such as Apress' own Practical Python, O'Reilly's Learning Python or the free online book How To Think Like a Computer Scientist (the Python edition).
ConclusionDive Into Python may be one of the thinnest programming language books on my shelf, but it's also one of the best. Whether you're an experienced programmer looking to get into Python or grizzled Python veteran who remembers the days when you had to import the string module, Dive Into Python is your "desert island" Python book. If you're new to programming but have heard all the wonderful things about Python, make sure that this is the second programming book you read. My congratulations to Mark Pilgrim on an excellent book and authorial debut!
(Remember, you don't have to just listen to my effusive praise. Dive Into Python is available for free at diveintopython.org. Read it for yourself and if you like it, vote with your dollar!)
You can purchase Dive Into Python from bn.com. Slashdot welcomes readers' book reviews -- to see your own review here, read the book review guidelines, then visit the submission page.
Dive Into Mark. Lots of interesting/useful stuff.
Here's a good link to diveintopython.org. The one at the end of the review is horked.
This book was awesome, I've been using it for awhile, and it's a really good python guide.
For all of you who still haven't seen it, I strongly recommend this 'propaganda' gem from the Python community:
Video: Introducing Python
Features GvR, ESR, etc.
It's so bad it hurts. You'll want to show it to all your friends.
A message from the system administrator: 'I've upped my priority. Now up yours.'
e.g, look at this super-terse, but still legible, sub-array code:List comprehensions are another great feature:This is so amazingly compact when compared with list processing in Java or C++.
Anyhow, hats off to Guido Van Rossum for such a great language. I'll have to check this book out... I've been using the python cookbook (OReilly) and the documentation on the site almost exclusively...
std::disclaimer<std::legalese> sig=new std::disclaimer; sig->dump(); delete sig;
--Jon
Cleanstick.org: Dumb weblog about nothing
Have you read The Practice of Programming by Brian Kernighan & Rob Pike? I thought it was a pretty good "why-to" sort of book that didn't spoon feed you anything, plus it gives examples/assignments in multiple languages so you can see the differences between them. If you haven't read this one, check it out. They probably have it at the library if you don't want to read it, but I'm glad I own a copy.
Those are my principles, and if you don't like them... well, I have others.
Groucho Marx
Indentation to create blocks is stupid when you begin to learn Python. And after a day or two you don't think about it anymore. The language is cool, and tabulation is just a detail when you've got OOP, regexpes...
Get a smart text editor; one that can handle this. When I cut'n'paste Java code in Eclipse, for example, it makes all of thew whitespace perfect. Indenting and outdenting code should be no more than a few keystrokes. ("<<" or ">>" in vim's visual mode. ctrl-[ or ctrl-] in Eclipse. etc.)
This is something I always did in other languages anyway, so Python's whitespace handling never bothered me. Improper whitespace drives me nuts (too hard to read), so a language that enforces proper whitespace is perfect to me.
here is a mirror
If you use ActiveState's free distribution of Python for Windows, ActivePython, the electronic version of Dive into Python is included in the documentation file under "Helpful Resources". Very handy!
Read my blog.
Google, ILM, and NASA all use Python. See the Python Quotes page.
Emacs python-mode makes adjusting indent levels for blocks trivial:
:-)
Paste or select a block, C-> indents, C- outdents
Don't bother selecting your whole file & re-indenting the whole thing.
Note that with a brace/delimiter oriented language, you often have to manually insert/delete delimiters...
C-M-\ (indent-region) still works to adjust indent levels (i.e., if somebody indented blocks w/ 2 spaces and your setup is 4 spaces), but it won't move code between blocks.
Entering new code in python-mode is also easy - it knows when to begin a block, to end a block you just backspace once-per-desired-outdent level.
TAB still indents a line to where emacs thinks it should go, backspace jumps back an indent level.
But the real power of python-mode is in executing code blocks in an interactive interpreter:
C-c C-c to execute the buffer
C-M-x to execute a function/class definition
C-| to execute the selected region
Nice.
kb
So here's the contents page from Google's cache:-
Dive Into Python Table Of Contents
there's py2exe... but that's just an executable with the python interpreter and all necessary modules and code included, IIRC.
---If you can't trust a nerd, who can you trust?
A good algorithm is just to put the first line at the current indent level and follow the indent/outdent patterns from there. So you position the cursor correctly and then it goes from there. If you make a mistake, you just hit the indent/outdent button after pasting.
I often in emacs select the whole file and tell it to correct the identation... which is impossible in python?
Which is unnecessary in Python. The indentation is always correct, and that's that.
practical python by magnus lie hetland. good stuff. written by a norwegian dude
why didn't the author think to utilize NYU's Coral content distribution network (covered here on /. just a couple weeks ago) to keep the bandwidth off diveintopython.org? Now it's too late, since Coral can't access the site to get it in the cache.
--Justin
http://www.artfiles.org/freebsd.org/distfiles/dive intopython/diveintopython-pdf-5.4.zip
Drooling to give it a read, but can't get to the site? The previous link was to a version from 2002. If you go to google and search for anything along the lines of "diveintopython_5.4.orig.tar.gz" you'll find the May 20th, 2004 version is on many linux distro sites. Enjoy!
### Right, and even worse, its inability to encapsulate class-local methods (no data hiding => no OO!).
I don't think that data hiding is all that important for OO, after all it doesn't add any functionality, it just restricts access a little bit and is in languages like C++ even easily workarounded via raw pointers or even more evil '#define private public' hacks. Worse of all, data hiding can make it actually impossible or very ugly to workaround bugs in a library.
Anyway, as far as I know Python does data hiding via '__' prefixes infront of the variable name, should be good enough to tell anybody 'don't touch this' while still allowing access if absolutly necesarry.
For those looking for some decent Python hosting (or PHP 5 even for that matter), I use http://www.a2webhosting.com/. They seem to know what they're doing.
And it includes all the documentation !
No, really, writing a lot of code in very few characters or lines is not the target, it is not good practice, it only makes code fuzzy.
As a small and simple tutorial guide I've enjoyed this book.
Python: Visual QuickStart Guide
ISBN:0201748843
Peachpit Press
Most of the examples are for the command line. This will encourage you to experiment and play. Creative play is where good programmers come from. It's not going to teach you how to write a big app or become a graphics wizard. It's certainly not suitable as your only reference book. It will give you an excellent introduction to the language in simple prose. It's small, lightweight, inexpensive and brief. It'll get you started, and you'll occasionally go back to it when you want a simple understandable explanation of a concept. I've bought some other tomes that weigh 10 times as much, cost 3 times and came with a CD, that aren't as useful. I like it, maybe it'll help you. I suspect it would be ideal for anyone who has trouble with english.
There are a variety of other data-hiding techniques. Enforced encapsulation isn't really an essential aspect of OO anyway; Smalltalk has no private methods (except by convention, like Python), and Smalltalk is quintessentially OO. Smalltalk does make all instance variables private; I'd probably agree that it would have been better if Python instance variables were private, but you can use property if you later decide to make a public attribute calculated, or one of the other cases where public attributes cause problems.