Learn Python

June 2nd, 2006

I have never gotten very good at any of the popular shell scripting languages. Over the years, I’ve dabbled in Bourne sh scripts, Perl, awk, and sed. I was never one of those magical guys who could make anything happen in a few seconds. But I’ve always wanted that to change.

I’ve decided to learn Python. I want a fast-acting Swiss Army Knife at my disposal for splicing together the loose ends of my computer life.

Why Python? I don’t know it well enough to really sing its praises, but a number of influences caused me to veer towards this language (as opposed to say, improving my Perl or picking up Ruby):

  1. A Killer App. NodeBox was the catalyst for this whole pursuit, though it has nothing to do with productivity, per se. I downloaded NodeBox on the recommendation of a friend, and was instantly blown away that such a tool exists at all. NodeBox makes it easy to programatically produce graphics on the Mac, exploiting the Quartz graphics capabilities of the system. Python happens to be the language for driving these amazing dynamic documents.
  2. A Good Reputation. I started dropping Python references among groups of programmer nerds who I respect. Almost universally people either didn’t respond (people like me), or else raved about how great it was. About the only thing I ever hear people complain about with Python is its strict indentation rules. Hey. I’m a C programmer, but I have style. Indentation is OK by me.
  3. Support From “The Man”. I’ll admit it, I am more likely to embrace something if Apple does it first. It’s just easier to get involved and stay involved with a technology when you know that there will be value-added by the most influential company in your little neck of the tech woods. Apple has included Python with Mac OS X as a standard part of the system, and even gone so far as to provide special Python bindings in frameworks like Quartz to facilitate the above-mentioned scripting tricks. I can embrace this language without worrying that its functionality on the Mac will suddenly disappear.
  4. A Decent Tutorial. The biggest roadblock to learning for me is finding the start of a good path. If there is an incremental process I can follow to get better at something and eventually “know it,” then it’s much easier to tackle. The author of Python himself, Guido van Rossum, provides a short-and-sweet tutorial that made it easy for me to get to know Python.

I have a little trick with these “short chapters” tutorials. I keep a bookmark to the “next chapter” in my Safari bookmarks bar, and add reading it to my ToDo list for the following day. When I finish the chapter, I update the toolbar bookmark and wait until the next day. That way the learning is never overwhelming – just a few minutes aday, and it still only takes me a couple weeks to get through the whole tutorial.

This trick is OK, but while I was taking this tutorial, I got annoyed by having to constantly flash back and forth between the web page and my Terminal window. The Python tutorial introduces you to ideas that, if you’re anything like me, you immediately want to try out in the interactive shell. I decided to take a cue from Michael McCracken, who recently produced and distributed a dumbed down web browser just for reading Gmail.

Learn Python is a simple Mac OS X application (GNU License, source included) that puts a web browser and terminal window into one application. The web browser points at the aforementioned Python tutorial on the web, and it remembers the last URL you went to, so you can trust it to remember which chapter you were at when you last quit.

It’s a Python-Tutorial Browsing Machine!

By almost every possible measurement, this project was a “waste of time.” But sometimes you’ve got to make the software you wish existed, just so you know it does. I rationalize the time as well-spent since it sets me up to easily crank out one-offs of a similar vein as I find a use for them. If I was really bored I would probably have added “value added” buttons to the UI for specifically navigating the document structure (e.g. next chapter, etc). But I think I’m done for now. Hopefully most of the bugs are ironed out.

I hope some of you will get some use out of this extremely narrow piece of work. At least it might encourage you to learn Python!

PS: Unhappy with the default version of Python that gets launched? Or wish you could use the tool as a ruby or shell scripting or whatever tool? I haven’t put any preferences in this application, but it would be easy enough to hack the sources. If you aren’t feeling up to hacking the sources, or don’t have the tools handy, you can specifically override the path of the launched tool by editing the file “Localizable.strings” in “Learn Python.app/Contents/Resources/English.lproj/”. Inside this file, change the line:

"/usr/bin/python" = "/usr/bin/python";

That will tell the application to use zsh instead of python. Replace the “/bin/zsh” with whatever you want.

Update (June 3, 2006): The download has been updated to version 1.0.3, which now defaults to a left-right orientation for the web and python shell panes. The setting can be changed back to top-bottom through the preferences dialog.

Update (June 4, 2006): Rodney Ramdas posted a modified version of the application that is targeted at learning Ruby, instead. I’ll probably use it to tackle Ruby, next!

43 Responses to “Learn Python”

  1. Will Koffel Says:

    This is just the kind of stupid app that might get my to learn Python, definitely a goal of mine. I share your reasons, for the most part. Lots of good engineers I respect rave about it, it has reasonably wide-spread adoption, and it seems to be a good tool at the command line, as opposed to PHP for example, which shares some features, but gets awkward quickly off-web.

    Perl has always been too much of a beast for me. I think you either *live* in Perl, or you remain forever in script-kiddie mode, never really using it for much more than simple log-parsing/regexp work, which is where I am. Maybe I can move past that same hump in python.

  2. Jacob Kaplan-Moss Says:

    I can’t suggest Dive Into Python (http://diveintopython.org/) highly enough — it’s by far the best way to learn Python, and perhaps the best “Learn $LANG” book ever written.

    Another reason to learn Python for us mac-heads: PyObjC, which I find a hell of a lot more productive than coding Cocoa in ObjC (no compilation steps…)

  3. Daniel Jalkut Says:

    Will: Glad you appreciate it :) We can compare notes on Python next week at CocoaHeads.

    Jacob: Yeah, I’ve had Dive Into Python recommended to me, but it seems more in-depth than the basic tutorial. Maybe after I “get my feet wet.” Heh, heh.

    The good news is that “Learn Python” should serve as a useful copilot with that or any other Python tutorial on the web. Just punch the URL in!

  4. Nathaniel Says:

    Certainly not a waste of time, I’m sure I’ll be using it myself to learn Python! Thanks!

  5. viperteq Says:

    Ok, I’ve got a question: see I’ve been mulling over which Programming language I should learn to compliment the programming that I do in ObjC/Cocoa (ok, actually, I’m learning ObjC/Cocoa…anyway). I wanted to learn a language that could be used in an OS as well as on the Web. My question is this: Is Python that language or should I bite the bullet and learn Ruby? From what I understand Ruby is mostly a programming language for the web, but it has functions built in that let a programmer “communicate” with the OS. Is Python built in the same way? I’d really hate to have to learn a shell script, ObjC/Cocoa and some scripting language besides (X)HTML/CSS. I feel like I’m beginning to get stretched too thin…….

  6. Andre Says:

    viperteq: ruby and python are very similar languages, both general dynamic languages. They are both capable of running web applications. They can both perform the same kinds of tasks. If you’re trying to decide between the two, it’s going to come down to your personal tastes, as most of the differences are attitude-based. The python attitude is generally that there should only be one really good way to do something. Ruby is closer to the perl mentality of tmtowtdi. (Don’t confuse perl and ruby, though. Ruby is a lot cleaner.) Python is extremely sparse with syntax. Ruby has blocks. Both python and ruby allow you to hot-wire code into an application at run time. In python, this is generally frowned upon. In ruby, it is the essence of getting things done. Python says explicit is better than implicit. Ruby says imlicit is better than explicit. If you find yourself nodding at any of those attitude statements, you’ve probably found your scripting friend. But there is no difference in capabilities. It’s not what you can do, it’s how you do it. (If you’re curious, I’m a ruby guy myself)

  7. since1968 Says:

    Good grief, what a cool little app. Thanks for posting this.

  8. dvb Says:

    Huh! I might just have to wander through the tutorial now. Been meaning to, you know…

    This is a very cool little application, it could have a lot of possible uses. You know when people start whining and making unreasonable demands and endless unsolicited suggestions that you’re on to something. So, let me prove you’re on to something with a single unreasonable demand:

    A home button. I was so fascinated by the fact that it was a for-real browser that I quickly wandered away from the Python tutorial. (Don’t worry ’bout me, I found my way back…)

    Maybe I’ll try to add one…

  9. Chris Thomas Says:

    NodeBox looks like a really cool riff on old Logo environments. Nice.

    I tried to enthusiastically take up Python before I found Ruby, but I always find Python it easier to avoid than to program, and not because there wasn’t a decent environment, and I don’t care much about the enforced indentation. (I mean, I don’t consider enforced indentation an issue while programming — I want my editor to take care of that for me anyway — though roundtripping code through email or the web is a different story — but I digress).

    Thing is, logically, Python *should* have worked for me. It’s elegantly designed (sort of); it has better Mac bridging capabilties out of the box (Mac OS 9 was a first class citizen, where Perl was shoehorned in and Ruby never made it there); it has lots of user support. But Ruby fits my brain like a glove. That’s not an uncommon assertion among Ruby programmers; it’s not a common one for Perl or Python (or C++) programmers.

    I have to assume the relatively immature state of OS X integration is the reason you’re avoiding Ruby. That’s a shame, because I find Ruby the best of these ‘little’ languages, in terms of pure brain-to-bits expressiveness. It’s also the most Objective-C like object model, sharing the common heritage of SmallTalk. Ruby’s block syntax provides cognitive efficiencies that Python users can only dream about.

    special Python bindings in frameworks like Quartz to facilitate the above-mentioned scripting tricks

    But this is implemented via SWIG, which also facilitates bindings for many other languages. The mystery is why they didn’t offer this support for Perl and Ruby (at least), since it’s not much more work to flip those command-line switches. (Yes, I filed a bug. Prior to Tiger release. A long time prior.)

    I can embrace this language without worrying that its functionality on the Mac will suddenly disappear.

    Seen AppKit Java lately? :)

    Anyway, you may ultimately find that programming Python is no fun, or at least not as much fun as you were promised. You should then grab a copy of the Ruby Pickaxe book (second edition).

  10. Daniel Jalkut Says:

    Thanks for the comments, everybody. I’m glad it’s having the desired effect of making it “just too easy” to go through the tutorial. Since it sounds like at least a few of you will use it, I’ll probably be inspired to put some finishing touches on it.

    dvb: Excellent idea. In fact, inspired by the “Dive Into Python” suggestion, I thought it might be a cool idea to start planning for “multiple homes” and put a popup menu into the UI with a list of all the tutorials one might start on.

    Chris: I appreciate your feedback. Truth be told, I’m excited about Ruby, too. It’s just not “front and center” at the moment. It’s true I sort of decided to focus on python first, mainly because of the reasons outlined above. But I’m interested in Ruby and am hoping that after I get Python under my belt I’ll be better equipped to take in some other languages as well.

    I didn’t realize the Quartz support was a “SWIG Binding.” That’s good to know, and I agree it would be cool if Apple helped make that accessible from whatever scripting languages.

  11. scott lewis Says:

    This is an incredibly useful tool. If it had been around back when I tried learning Python it would have saved me a lot of Cmd-tabbing. There’s only one small point I would suggest changing: make the divider vertical rather than horizontal. That way you can see more context in the tutorial at every given point on the shorter screens (12 and 13 inchers). (screenshot)

  12. Daniel Jalkut Says:

    Scott: thanks! And of course I think you’re right … rarely do I care for a wide output on the Python window, and if I do I could just resize it. Good idea!

  13. eBob Says:

    OmniWeb will remember where you were last and bring back all those windows, on the pages they were at when you quit. Plus, because of the workspaces feature, you can “push aside” your Python reading in order to do normal browsing.

  14. Joe Says:

    I’d like to second the “Dive into Python” site for another great online learning site. Although Mark Pilgrim says some things that I periodically disagree with, he’s written a great tutorial overview.

  15. Arto Bendiken Says:

    Originally coming from a C / J2EE background myself, Python was the first dynamic programming language I learned (aside from PHP, but that hardly counts). Python is a great language, and just what I needed at the time to start realizing the massive design flaws inherent in Java and the bloated, overcomplex frameworks built on top of Java.

    I worked a lot with Python, in the following couple of years. The indentation never bothered me at all, I mean, I would indent the code anyway, so what’s the problem? Dynamic typing felt like a liberation, first-class functions were intriguing, list comprehensions and the like had a lot of expressive power, and the excellent Twisted Matrix framework was a true boon for developing networked apps.

    If I had to criticize something about the language, it would be that the object-oriented features started to feel rather bolted on (the first clue being the explicit “self” parameter to class methods), and the standard library seemed to lack quite a bit in uniformity and consistency of naming conventions etc. (though not as badly as PHP’s).

    As I was a happy camper with Python, I resisted learning more about Ruby though I kept hearing about it and running into it. Ruby’s block syntax seemed weird, and besides, I didn’t really grok what blocks were anyway, yet. Eventually, the first Ruby on Rails release arrived and I did have a look. It didn’t take me long to realize that Ruby was a lot like Python, with some Perl (and as it turned out, Smalltalk and Lisp) thrown in for good measure, and that it, simply said, was a lot more powerful and expressive than Python.

    Since discovering Ruby, I’ve done almost no Python programming; it feels limiting, though not quite in the degree that having to program in, say, Java would feel. More importantly, Ruby also opened the door to yet more powerful languages, namely the Lisp language family, though I’ve picked up some Haskell etc. along the way.

    I won’t extoll the virtues of Ruby further, here. I’ll just say, sure, learn Python. But after you’ve come to good grips with it, and before diving much deeper into it, have a serious look at Ruby.

    As Andre commented above, for some people, the choice between the two may be influenced by the attitudes apparent in their respective communities (namely, the “totalitarian” Python vs “anything goes” Ruby). For others like me, who care about solving their problems with the least lines of code, the power inherent in the design choices of the language itself is yet more important: the one providing the most powerful abstractions (i.e., the one most approaching Lisp) wins. Whichever camp you fall into, just try and make the decision early enough so that inertia won’t have had time to set in, and so that, lacking a large investment in any of the contenders, changing your mind is still is easy.

    Good luck on the journey, it’s sure to be an interesting and mind-expanding one! I look forward to reading more about your progress.

  16. Joachim Bengtsson Says:

    Dude, NOT waste of time. Actually, you should release this as a real app, as it’s not only useful for Python, but any language. Throw http://www.rubycentral.com/book/ into the left pane, and /usr/local/bin/irb into the right pane, and wham! A Ruby learning tool.

    Add just a teensy bit of configurability (that is, choosing interpreter (python, ruby, shell, other) from Preferences), rebuild and put on Versiontracker. Definitely.

    (Hm, ‘tho arrow keys don’t seem to work…)

  17. algal Says:

    If you’re getting involvd in Python, here’s another quick piece of advice. Once you’re comfortable, ditch the default python interpreter “python” and check out “ipython” instead. It doesn’t change the language, it doesn’t actually replace the interpreter, it’s just a wrapper that runs around the interpreter and gives you a much more powerful development environment at the prompt.

    You can easily introspect objects, browsing their documentation and attributes. You can refer to earlier lines. And it’s easy to try: just one “fink install ipython-py24″ away. And if you don’t like it, “fink purge ipython-py24″ and it’s gone. I’ve downloaded foreign libraries, and learned how to use them just from browsing them and their examples in the environment. No readings docs! A very nice and interactive way to learn.

    Ruby is better for webapps, because of the high quality of Rails and because it attracts all the Ruby community attention. Most python users are not doing web apps, and there are (unfortunately) competing web app frameworks in Python world (Django, TurboGears, etc.). Ruby’s packaging system is slightly better. But ont the whole, Python has more libraries, deeper libraries, and better vendor support at this time. This may change as the Rails juggernaut builds momentum.

    As regards language features, Ruby is often sold as being more “dynamic”. This claim is usually overstated, partly because the Ruby programming culture is more oriented toward dynamic features, and partly because many Rubyists are enthusiasts. The most substantive comparison I have found is by Ian Bicking: http://blog.ianbicking.org/ruby-python-power.html . Peter Norvig also has a good comparison of python with Common Lisp, if that’s a useful reference point for you.

  18. algal Says:

    And by the way: I second Joachim’s request that you tweak this app so it could also be a Ruby-tutorial browser!

    with love,
    Lazy Web.

  19. Rodney Says:

    Hi Daniel, I’m green with envy, this is indeed a useful little proggie. As per your suggestion I hacked it to show the Ruby Pickaxe book and an interactive Ruby session. I posted it here. Please don’t be mad :-) Next step is to have a 4-paned window with both Python and Ruby running alongside each other like old friends.

  20. Daniel Jalkut Says:

    Thanks for the great comments. I have been feeling the pull towards branding it as a “generic” browser/shell tutorial app. But what do I call it then “Shell Tutor” ?

  21. Daniel Jalkut Says:

    Rodney: No problem at all with the innovative “Learn Ruby” development. Hopefully one of these days I’ll customize the original app enough that it’s easy to set it up for whichever language.

    I appreciate that you changed the name and icon of the app so users don’t get confused with the original. The only other thing I’d ask is that you change the “Bundle Identifier” of the app so that it doesn’t get confused by the system with Learn Python (it will save preferences etc to the same file):

    1. Open “Learn Ruby.app/Contents/Info.plist
    2. Edit the “CFBundleIdentifier” to something like “com.pinupgeek.learnruby”.

    Thanks!

  22. Niklas Nisbeth Says:

    Fantabulous. I Know What I’ll Do This Summer.

  23. kelly Says:

    this is a link to a Quick reference for python.

    http://rgruet.free.fr/PQR24/PQR2.4.html

    this is the reason for me learning python.

    http://www.panda3d.org

    mac support is on the way.

  24. import this. » Blog Archive » NodeBox Says:

    [...] It’s also one of the reasons to learn Python according to the Red Sweater Blog. [...]

  25. André Roberge Says:

    This looks really nice but, unfortunately for me as a Windows user, I can’t use it – and not even have a peek at the source code :-( I am working on something related, yet orthogonal to your app. My app is called Crunchy Frog, and it allows you to insert an interpreter (or even a primitive editor from which you can run Python) inside a web page – rather than side by side. Of course, this requires the web page (read: Python tutorial) to have some small additional markup in it. Now that you know Python, feel free to try it out and let me know what you think;-)

  26. Daniel Jalkut Says:

    André: It’s true that you can’t run it on Windows, but you should be able to look at the source code at least, what little of it there is. Much of the functionality came “for free”: the web view is from Apple’s standard WebKit (analogous to Microsoft’s embedded IE views), and the terminal view came from an open source GNU Terminal application. I just added some preference saving and common-sense UI polish.

    Your project looks really cool! I also saw another project mentioned on the “Learn Ruby” link that lets you try Ruby right in a browser. Good ideas, all around. I will give Crunchy Frog a try…

    By the way, the best solution to running Windows and still being able to try cool Mac stuff is to buy a Mac :) The MacBook is apparently one of the best performing Windows PCs you can buy for the price! Then you can just boot into Mac when you feel like testing the waters.

  27. Kevin Horton Says:

    The info on how to change the python version (or point this idea at another scripting language) is slightly incorrect (or out of date). With Learning Python 1.0.3, you need to edit the file “Localizable.strings” that already exists at “Learn Python 1.0/Learn Python.app/Contents/Resources/English.lproj”. The path given above is incorrect.

    Thanks for the app.

  28. Daniel Jalkut Says:

    Thanks Kevin – yeah I added the file shortly after putting the advice here. I thought it would be easier to change if there was already something there.

  29. Kevin Horton Says:

    “yeah I added the file shortly after putting the advice here. I thought it would be easier to change if there was already something there.”

    Putting the file in place was a good idea. Otherwise I would never had figured out how to change to Fink’s python, as I was looking in the wrong place.

    You need to change the path in the instructions above. Remove the “/MacOS” from the path. I followed the instructions in your original item, they didn’t work, then I dug around and found the Localizable.strings file where it actually is.

  30. Daniel Jalkut Says:

    Oops! I glossed right over the fact that you were identifying a mistake in the path. Thanks…

  31. Mike Says:

    Cool – see also http://www.pycs.net/bbum/2005/2/21/ for some tips on how to get a comfortable python environment started – specifically the readline support, which isn’t standard in OS X, and makes the python interpreter *much* easier to work with. (it provides the line editing and command history that you expect from a shell)

  32. Ben Says:

    Dude. You rock.

  33. IVfx | Web Design, Graphic Design, and Illustration » Have You Ever Kissed a Snake? Says:

    [...] After a few weeks of sitting pouring over a number of options on how to install Django on my Mac Desktop, I finally have succeeded in getting Django up and running! Why does a web designer want to get into Django? I pretty much blame the blog post by Jeff Croft I read back in May. I’d heard of Django prior to his post, but the examples he gave really caused me to seriously think again about the use of Python and Django when developing sites or web applications. However, I wondered if this 36-year-old father would have the time, brain-power, and wherewithal to accomplish the task of learning Django, let alone Python. I do want to make a bit of a side note here. A few weeks back, I went through an online tutorial on Ruby and found it to be pretty amazing. I also couldn’t believe how straight forward the syntax was – much easier than my days when I tried programming in Pascal. Soon after, I started reading articles on how Ruby was great, but didn’t quite compare to Python. Those articles, along with how Python stood head and shoulders above PHP, caused me to take more than a casual glance at Python. I then found a great little application to learn python, developed by Daniel Jalkut. So, with all the information I needed to persuade me to try out Python, I felt I needed to take the plunge and see if I could take on the challenge of developing using Django. [...]

  34. niso Says:

    hi, when unzipped i only found a version for mac, seeing as im a windows user is there a verson for windows? thx

  35. Daniel Jalkut Says:

    Sorry Niso – I’m a Mac user only, so it’s not available for PC. It would be a good project for somebody to try on the PC, too, I think!

  36. } KUNGFOOO { » Python on Mac OS X - The Essential Collection. Says:

    [...] Learn Python {Brilliant Mac specific tutorial/reference browser with a built in Python prompt} [...]

  37. News from above » Blog Archive » Python tutorial Says:

    [...] http://www.red-sweater.com/blog/140/learn-python [...]

  38. wagthis.com Says:

    Red Sweater Blog – Learn Python…

  39. Joe Frost Says:

    Superb app, can’t believe nobody thought of it before. The idea of substituting a basic shell opens up no end of possibilities for using all of the how-to and install guides that are on the net.
    Open up a telnet connection to another host (Parallels Solaris/Linux etc), work through installing/compiling (some app) with the user guide right in front of you. Paste in the commands straight from the web.
    This little program should be in everyone’s toolkit.
    Thanks for your work, it is much appreciated.

  40. Clay Wiedemann Says:

    I wanted to tweak this to use ipython, but the file editing you suggested above did not work for me. Is the explanation complete? It references zsh but does not show it in the example. Nothing I did changed much.

    I was wondering, why not just open a terminal session and from there one could go into any editor. I do not know much about Cocoa or ObjC, so my monkeying around was fruitless.

  41. Daniel Jalkut Says:

    Clay: That part of my explanation was confusing. The end result should look something like this:

    “/usr/bin/python” = “/bin/zsh”;

    If you want to use zsh. That is, change the value on the right to the full path of whatever shell tool you want to run.

    The reason it doesn’t do this by default is it’s called “Learn Python,” and not “Leran Shell Stuff” so naturally I wanted to make it as purely functional for the intended purpose as possible. :)

  42. What's in my Dock Says:

    [...] myself the programming language, Python and I have a neat little utility in my dock called, Learn Python which combines a html tutorial window and a command line window. Next is a very small app called [...]

  43. Monday morning Apple links | Ars Technica Says:

    [...] Jalkut created an application that he's using/used to learn Python and has made it available to the public at large: "Learn Python is a simple Mac OS X [...]

Comments are closed.

Follow the Conversation

Stay up-to-date by subscribing to the Comments RSS Feed for this entry.