Learning Python

January’s Tech Theme of the Month is going to be Python.  I realize it’s still December; I decided to get a head start.

Last month’s tech theme was videoconferencing.  I learned a lot, including the unfortunate continued split between low end and high end and general lack of ability to have a single universal solution.  Oh – and bandwidth still matters a lot.  I expect by the end of January we’ll have much better videoconferencing infrastructure set up at Foundry Group with the single goal of eliminating some travel.

I’ve thought about learning Python for a while.  I don’t code much anymore and I regularly find myself wishing I could do something with a simple UI and heavy back-end processing – mostly to move data between web services that I use via the web services APIs.  I stopped programming around 1993 although I occasionally had to dive back in and support something that I had previously written until the late 1990’s, when I stopped for good because I simply had no time.  As a result, the languages I feel like I have mastery over are BASIC, Pascal, Scheme, and Dataflex and the corresponding environment that I’m comfortable developing in ends with MS-DOS and Novell Netware.  While I did stuff in plenty of other languages as a result of courses I took (IBM 370 assembler, SAS, Fortran) or projects  I had to figure out (PL/SQL + Oracle, Paradox, dBase), I don’t feel like I did enough with these to claim mastery.

Every couple of years, I fuck around with a new language and environment.  PHP is the one that has stuck the best and I can read it and hack around if necessary.  But I don’t really like PHP – it feels sloppy and I constantly am having to look up syntax because it’s not comfortable to me.  I went through a “ok – I’ll figure out Ruby on Rails” phase a few summers ago but stalled when I realized that Rails wasn’t a particularly practical environment for what I wanted to play around with.

Python may be a miss for me, but when I look at Python code I feel very comfortable with the syntax.  A few folks that I know that are like me (e.g. not developers anymore, but were once, and occasionally bust out an IDE to hack on something) swear by Python.  But the biggest motivation for me was that 6.01 is now taught using Python.

In 1984, I took 6.001: Structure and Interpretation of Computer Programs.  This is the first computer science class most MIT students take.  I had written a lot of software, including several commercially released products, almost entirely in BASIC (with a little Pascal and assembly.)  6.001’s programming language of choice was Scheme (a LISP dialect) and working with it was an epiphany for me.  I continued to write commercial apps in BASIC and started using a 4GL called Dataflex, but my coding approach was heavily influenced by what I learned in 6.001.  The abstractions you could build in BASIC, if you tried, were surprisingly good and Dataflex was a remarkably robust 4GL – very UI constrained, but very powerful (and fast compared to everything else at the time.)

So – if you look at my history, I’m comfortable with imperative languages.  I got a good dose of functional programming from MIT but never did anything with it commercially.  And I stopped developing software before object-oriented programming became popular.  Python feels like a mix of imperative and functional when I look at it and read about it so I’m optimistic that I can use my regular programming muscles without having to fight through the OOP stuff that I don’t really know and doesn’t feel natural to me.

MIT has an IAP course (the MIT January session) titled 6.189: A Gentle Introduction to Programming Using Python.  As with many MIT courses, it’s available on MIT OpenCourseWare so I’m going to take the course over the next month and see how it goes.

If you are a Python expert and have any suggestions for sites, tools, or blogs I should pay attention to, please leave them in the comments.

  • The sweet-spot for Rails is database-backed web apps, especially new ones with a lot of interactivity. It’s a really, really fast framework for doing this cleanly.

    For hitting web services, you could ditch rails and just use Ruby, perhaps even from the command-line. Ruby is what makes Rails magical and powerful.

    Either Ruby or Python are fine for that use case though, so it’s mostly a matter of what you like and know.

    • I found Ruby syntax to be too difficult for me. I’m sure it was all
      the OOP stuff, but I kept getting lost whenever I tried to do anything
      that was beyond straightforward.

      • Rails is not the most straightforward, I second that. I suggest starting out with pure Ruby and the Sinatra framework. The most simple and beautiful code I’ve ever seen.

  • Jeffrey McManus

    Our site, CodeLesson, has an instructor-led Python course that’s terrific: http://codelesson.com/python

    Lots of people can get pretty far with Google and the reference documentation on their own, but at some point you might want to be able to engage with a real human instructor who can answer your questions quickly. That’s what our courses provide.

    • Cool – I’ll take a look. I see it’s $295 / course so I’ll probably
      try some of the free stuff first.

      • Jeffrey McManus

        I guarantee that having access to an instructor who can answer your questions will save you enough time to make it worth your while.

        Just Googling stuff and reading the manual isn’t the same as taking a real class.

        By the way, the instructor of the CodeLesson Python course is Zed Shaw, who wrote “Learn Python the Hard Way,” which a few of your other commenters also recommended. For his course Zed’s added some hands-on exercises, quizzes and discussions.

  • Learn Python in tandem with App Engine. New light bulbs will go on for how easy it is to get a webapp up and running these days.


  • Dan O’Keefe

    Its awesome that you are keeping your feet wet considering your developer background.

    When you tried RoR, what was it about the environment that led you to conclude it was not for you? I am considering giving that a go as well since that seem’s to be the de facto standard for startups having a good code foundation to start with.

    • I didn’t like the contraints of the Rails Framework and I struggled
      with the syntax of Ruby. Plus, most of what I wanted to do didn’t
      require much of a UI so I ended up fighting against the strongest
      reasons to use RoR.

  • raj

    Learn Python the Hard Way is a great way to start. Dive Into Python is great too.



  • Tom

    I just could never get used to white space being part of the syntax of the language. And I don’t think I ever will.

  • Cool didn’t see that intro course on ocw. Making a point to learn python too. I second the recommendation for Learn Python the Hard Way.

  • I like Python because it makes sense. Extraneous syntax is gone and you can concentrate on being productive.

    My first choice is to code in Python, but I also find myself using Java and Java-like code when writing for cloud platforms. Force.com is particularly interesting – it forces 75% test coverage! Seriously, if you want to deploy inside the Force.com cloud, the code must have 75% test coverage and otherwise it simply won’t run. You might as well go for 100% at that point. They also require a mandatory security review. As I am building a Force.com application, I had no choice but to become a much better programmer. 🙂

    There are many resources I used to get proficient.

    On youtube, check out
    http://youtube.com/googlecode http://youtube.com/googledevelopers http://youtube.com/thenewboston (amusing tutorial style that teaches syntax from zero)

    I would recommend the following books, to which I have access on Safari books online via my library card. You can also subscribe to the service if you wish to pay for it.

    The Object-Oriented Thought Process, Third Edition
    Clean Code: A Handbook of Agile Software Craftsmanship
    Growing Object-Oriented Software, Guided by Tests
    Code Complete, Second Edition
    No list would be complete without mentioning Design Patterns: Elements of Reusable Object-Oriented Software

    Most of them are written referencing Java code, but the concepts will apply to Python. These books are great for getting into OOP mindset and into writing extremely readable code.

  • I’ll second @raj’s recommendation for http://diveintopython.org. It’s a very well written Python-for-programmers 101.

  • while it’s hard to build heavy apps on Google App Engine… it’s DNA is in Python (first supported language) and the framework is really fun to use with Python… the DAO layer, the web framework, and associated utilities give you a pretty good up-and-down the stack view.

  • You probably can’t go too far wrong with the Google intro to Python course that they offer to non- programmers they hire http://code.google.com/edu/languages/google-python-class/introduction.html

  • Eric

    My CS TA recommended Dive Into Python when I was asking about good resources, looks like other comments here suggest it as well.

  • tusuzu

    Yes, as someone who learned to program on punch cards, had moments of anxiety when C turned into C+ then C++, had to use ‘Move’, ‘Push’ and ‘Pop’ statements to access data, and still has some recollection of lengthy CDR and CAR constructions (LISP), I too stopped programming right at the beginnings of OOP. I began working with networks. Wrote a lot of scripts for data center maintenance. Finally jumped into a project and began to work with Python last May. You’re going to like it a lot.

  • pbreit

    I second (third? fourth?) “Learn Python the Hard Way”. Then, a great little framework for quickly building database-driven apps is Web2py. You can run it right on your Mac as well as deploy to remote servers. Simple to get started yet very powerful.

  • Brad, I think you actually chosen the perfect language. I come from an assembler and C/C++ background in the embedded domain, and I found Python being a phenomenal language. I can do a lot of prototyping (and real production code) very fast in Python; it would literally take me 10 times longer in C.

  • Anonymous

    You’re not so far behind.

    Beyond the programming language itself, I believe that now the main consideration is what APIs the language can use. So, I’ve settled on Visual Basic .NET because as a compiled language it is okay and it gives nearly full access to .NET, which is enormous, ADO.NET for getting to relational databases, ASP.NET for the server side code for Web pages, systems management and administration on Windows, etc.

    So, I’ve selected the Microsoft world instead of the Unix world — there are pros and cons here.

    Broadly, then, I don’t see that the ‘language’ is one of the main points. Instead, the main points are the hardware (e.g., Intel-AMD 32 and 64 bit processors and the Microsoft PC standards), the operating systems (e.g., Windows XP, 7, Server 2008), the APIs (e.g., .NET), and the functionality, documentation, support, popularity, and future of these.

    For languages, starting with Basic, welcome to the club. I started with Basic and for compiled languages continued with Fortran (still has some advantages, and it’s grown). Algol (NICE language), a few assemblers, a lot of PL/I (very nice language), C, and now Visual Basic .NET.

    And I’ve used several interpretative and scripting languages. The nicest so far is ObjectRexx.

    For the MS/DOS development environment, there is a good point: So far all the languages I’ve seen want the source code just as simple text in a file. So, if you typed software into a good text editor on MS/DOS, then you can continue to do so now. An ‘integrated development environment’ (IDE) has some advantages, but they tend to be specific to only a few languages. Yes, more generally, I emphasize having a good text editor and then using it for as much typing as possible.

    Yes, the IDE idea of putting up a window of documentation for each object method call, etc. is helpful. My solution is to have the documentation on my hard disk, put the tree name as a comment in my code, and have one keystroke in my editor to display the documentation. There’s more than one way to get the job done!

    Net, you can continue to do quite well on Windows typing software into a text editor much like you did on MS/DOS.

    Indeed there is a theme widely visible on the Internet: Many experienced programmers prefer command lines because (1) they are easy to script, (2) they are explicit, (3) they aid in automating work. So, such command line usage goes back to MS/DOS.

    For database, relational database is NICE, and so is the SQL language (that at least Microsoft calls Transact SQL or T-SQL). If you write Visual Basic and use ADO.NET, often there is little new to learn since you are just typing T-SQL into Visual Basic strings.

    A scripting language is important. Apparently on Windows the intended future is PowerShell. I’m still getting what I need from ObjectRexx, but I will likely move to PowerShell with its promise of good access to much of Windows system management, etc.

    For ‘object oriented programming’ (OOP), you didn’t miss much: A start on it was common in, say, just Fortran. E.g., suppose read Knuth on random number generators and see that want to calculate

    X1 = X0 * A + B mod C

    where everything is a 64 bit non-negative integer. The A, B, and C are fixed for the one random number recipe, and one of the Knuth recommendations, right, from Oak Ridge and Coveyou and McPherson, is A = 5**15, B = 1, and C = 2**47.

    So, for one such random number generator, have a Fortran array and in it store X0, A, B, and C. Pass the array to the function for the calculation and have it put X1 in the array in place of X0 and also return the floating point number X1 / C in the interval [0, 1).

    So, where is the OOP? Well, the array is one ‘instance’ of the ‘object’ and from the ‘constructor’ that you got when you ‘declared’ the array and, thus, had Fortran allocate it. With some dynamic memory allocation in Fortran (several options), your allocation would be the ‘constructor’. The function that does the calculation is the ‘method’ for the object. The ‘object’ or ‘class’ is the code and all the instances allocated.

    I did this because I wanted to (1) write the function once and have it work for any of several recipes in Knuth, that is didn’t want to ‘hard code’ choices for A, B, and C in the function, and (2) be able to have several random sequences from just one such ‘recipe’.

    Or the ‘code and data’ are separated and can have many instances of the data and just one instance of the code. That idea is the core of OOP.

    Yes, N. Wirth has said that OOP is still “call-return semantics”. Yup.

    Fortran had another part of OOP: So, commonly there were routines to, say, find a numerical solution to an initial value problem for an ordinary differential equation, e.g., hit a baseball and calculate where it will be each, say, 100th of a second.

    So, someone would write a general purpose differential equation solving routine. But the equation may have a function particular to the equation that the solver needed. So, the user would pass to the solver the function, and the solver would call the function.

    OOP is an old idea: IBM had it in microcode in the early 1970s.

    But OOP got going heavily because of the popularity C, how little the language had, that it did have dynamic memory allocation and pointers, and that it did have what it called ‘structures’ (nowhere nearly as powerful as PL/I structures). So, C programmers were commonly getting around how little C had by using the structures to be the storage for ‘instances’ of objects just as in the random number generator.

    Then Stroustrup wrote a pre-processor to C to enhance and formalize what people had been doing.

    The ‘objects’ in Visual Basic are relatively simple and, for ‘polymorphism’, depend heavily on ‘interfaces’, and these are close to the Fortran idea of passing a function to a routine. The vanilla application, and case of polymorphism, is to write a sort routine that will sort arrays of anything. So, for the comparisons, the interface passes a comparison routine to the sort routine, and the sort routine calls the comparison routine much as for the differential equation solver. So, the obvious OOP overhead is having to call a function for each comparison in the sort.

    For more in polymorphism Visual Basic can use ‘reflection’ (apparently having the execution look at the symbol table, etc.), have ‘late binding’, and be more ‘interpretive’. So, can write a sort routine that runs in 3 seconds. If do interfaces and polymorphism right, it will run in about 7 seconds. If let Visual Basic use ‘late binding’, the routine can run in 600 seconds. Been there; done that. The timings are real.

    In building an API, OOP can be good to simplify what the user of the API has to see.

    Here I’ve given just the basics of OOP. There is much more, especially on ‘inheritance’ (computing is awash in various cases of inheritance). And, yes, there has been a lot of hype and the usual programming language religious arguments.

    For inheritance, .NET has a huge inheritance hierarchy, but so far I’ve managed mostly to ignore it.

    Uh, my code is SIMPLE. One reason is, I want the code to be fast for my business and easy to understand for me, this last because when it breaks, guess who gets to fix it?

    If want more on OOP, can try:

    Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides, ‘Design Patterns: Elements of Reusable Object-Oriented Software’, ISBN 0-201-63361-2, Addison-Wesley, Reading, Massachusetts, 1995.

    Grady Booch, ‘Object-Oriented Analysis and Design with Applications’, second edition, ISBN 0-8053-5340-2, Addison-Wesley, Menlo Park, CA, 1994.

    James Rumbaugh, Michael Blaha, William Premerlani, Frederick Eddy, William Lorensen, ‘Object-Oriented Modeling and Design’, ISBN 0-13-629841-9, Prentice Hall, Englewood Cliffs, NJ, 1991.

    Sally Shlaer and Stephen J. Mellor, ‘Object-Oriented Systems Analysis: Modeling the World in Data’, ISBN 0-13-629023-X, Yordon Press, Prentice Hall Building, Englewood Cliffs, NJ, 1988.

    Sally Shlaer and Stephen J. Mellor, ‘Object Lifecycles’, ISBN 0-13-629023-X, Sally Shlaer and Stephen J. Mellor, ‘Object-Oriented Systems Analysis: Modeling the World in Data’, ISBN 0-13-629023-X, Prentice Hall, Englewood Cliffs, NJ, 1992.

    These sources were from the time of high hype of OOP, and there are no doubt more recent sources. Own them? Yes. Like them? No!

    I mean, there has been functional programming, structured programming, OOP, and more.

    In spite of all of this, today a LOT of applications programming is just a little glue to hold together calls to APIs written by others.

    At present it appears that Microsoft no longer much cares about programming languages, regards them all as just different flavors of syntactic sugar, is concentrating on their ‘common language runtime’ (CLR), .NET, etc. and is willing enough to have others write whatever new flavors of syntactic sugar icing on the cake if they wish.

    There has long been a conclusion that the actual programming language didn’t mean much and that what mattered more were the ‘platform’ the language used and the ‘application design’ the language implemented.

    So, I’m not asking much from Visual Basic .NET, and it is easily giving all I’m asking. Thus, similarly I’m not much interested in Python, Ruby, Haskel, etc.

    Of course, if I had selected Unix instead of Windows, then of course I’d need something to replace Visual Basic .NET.

  • Brad – If you have not yet, I suggest checking out the NLTK(Natural Language ToolKit) http://www.nltk.org/ a Python library for language processing. The NLTK documentation coupled with standard Python help is a fantastic way to learn the language and pick up some useful linguistic analysis techniques.

  • David Ward

    Your definitely welcome to stop upstairs of the Foundry office in Boulder, I’ve been using Python since 2007 and been exceedingly happy with it as a professional toolset.

    -David W.

    • Awesome – I nominate you as one of my sherpas as long as you are willing.

  • Candide

    If my hunch is right and you’re hacking together something with you QuantifiedSelf devices, I’m extremelly curious to know what it is. I am myself a Self Quantifier from Brussels and I am working on something that will centralize all my QS Data in one place. And BTW, I know you probably hate java, but you should have a look at Spring 3 and JRebel (which lets you use java just like it was interpreted). It is very effective at hacking stuff quickly.

    • Yup – one of the things I want to do is deal with all of my QuantifiedSelf data.

      I can’t stand Java – too complex.

  • Hah

    while it’s hard to build heavy apps on Google App Engine… it’s DNA is in Python (first supported language) and the framework is really fun to use with Python… the DAO layer, the web framework, and associated utilities give you a pretty good up-and-down the stack view.

  • Lee

    I just could never get used to white space being part of the syntax of the language. And I don’t think I ever will.

  • Harry Forsdick

    I looked at the MIT OCW website and don’t quite understand how it works. Will the lectures in the IAP course be posted there after they happen? Or even better, are they streamed as they happen?

    • They get posted after the fact. I’ve actually decided to do 6.00
      (instead of 6.189) since all the lectures from 6.00 have been posted.

  • Brad, It’d be great if you can keep us posted on this. I’m in basically the same boat as you are and have hankered off and on for a tool I could use to do some programming for many years. I think the last real programming I did was back in the 1980’s (BASIC, Pascal, Assembler, OPS5 and a few others I was comfortable with.) Tried Rails, Java, etc. but nothing clicked. Same feelings about PHP, I use it some because it works but it’s more like a macro/command language than a programming language. The ideas on leveraging some API data with backend processing to generate some interesting results have stayed on the drawing board because some decent programming is needed and the projects are too experimental to spend money on developers.

    • I will – I plan to write about this regularly as I go through the month.

  • Python is also the language most often used by hackers these days due to its broad support, quick prototyping and a powerful library. Here’s a web server in python:

    $ python -m SimpleHTTPServer

    (This opens a port on localhost:8080 and serves the current file tree.)

    Brad, if you ever want some live help, there are plenty of folks at CU Boulder who are into python and love to code (including me).

    • Thanks a ton – I’ll holler if / when I need help.

  • You’re going to love python. I recommend the book Python For Software Design: How to think like a computer scientist (available free online):

    This book was used by my python class at the University of Washington, and I was impressed by how it presents the language clearly in so few pages. Enjoy.

  • Thanks for sharing this, especially the pointer to the 6.00 class on OCW. I’d gotten through a few of the Stanford lectures but learning Java wasn’t really of interest so I had trailed of. I’d been working through the Python Tutorial and picked up a book or two but this class looks great. Love that all the readings are free and linked to from the course page. http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-00-introduction-to-computer-science-and-programming-fall-2008/readings/

  • Excellent decision… but one thing that is as important as the language is the tool to write in. Meaning your IDE. I have been a long-time user of IntelliJ IDEA for Java and I use it for Python programming too, using the Python plugin. But they have released a Python-specific IDE called PyCharm (http://www.jetbrains.com/pycharm/) Give it a look. It is the best Python IDE you can find.

  • Anonymous

    I love this post. It brought back so much memory. When I was a CS student at Cal Berkeley, we also used Structure and Interpretation of Computer Programming as the textbook for the first CS intro course. I absolutely fell in love with the elegance of the Scheme/Lisp programming language.

    It’s interesting that now MIT is switching to use Python to teach the class. I wonder if the text book will be updated with Python as well …

  • JonBirdsong


    Saw you signed up on OpenStudy. Any question you have, the community of learners will help you through the concepts in real time. Here’s the intro video:

  • Anonymous

    Good luck with Python. I love it, and I bet you will too. You’ll be amazed just how easy it is to do pretty complex things.

  • When it comes to building something for the web, take a look at Django (http://www.djangoproject.com), think of it as what Rails is to Ruby. It’s a bit nicer in a couple of ways when it comes to web apps, as it has authentication and administration functionalities built in out of the box. Its also easier to get out of the way of the framework a bit easier than it is with rails. But above all else the documentation for it is great compared to that of the rails community (http://www.djangobook.com).

  • Hey Brad, although I certainly wouldn’t discourage you from learning Python, have you looked at PHP’s OO stuff?
    It has helped me keep my php code a lot cleaner and coupled with using an MVC pattern I’ve been really happy with the outcome and lack of that sloppy feeling when looking at the code later 🙂

    The “problem” with php seems to be that it feels so sloppy yet it is always to gratifying to hack on 🙂

    So … my code looks a lot like this these days

    output_array = array();
    each of the M V and C classes output to the to the array.. can be associative or just a plain old array.

    controller class(output_array)
    Handles incoming web request mapping or app command args (yes I use php on the command line too) and then instantiates the correct model class and correct view class. After model class is “done” calls view class to output a desired representation of what the model just did..

    model class(output_array)
    abstracts database, file system and processing problems.

    view class(output_array)
    view knows how to converts the output that is sitting in ‘output_array’ to html or json or whatever the final representation needs to be. Could be: “send a file to the browser”, “render an image to output”. Think different view classes for different output formats.


    • I haven’t every really grokked PHP’s OO stuff because I’ve never
      really grokked OOP.

      • Ah. okay, thats not a problem. Its just to help organize (but *not* force into awkward class hierarchies and traditional oo thinking).
        In the past year I’ve really woke up too oo + mvc has an awesome combination of organization and simplicity.

      • oh, and I suspect the oo + mvc thing can work equally well in python 🙂

  • Interesting reading, I’m still trying to get my htc evo to video chat with qik