Sunday, 28 March 2010

Adventures in Python

I've decided to spend some time learning how to program, and as I encounter problems (and, hopefully, solve them!) I'll drop notes here on my blog in case they help others. Plus this has the added benefit of giving my developer friends a steady stream of entertainment....! And finally, in the style of Julie Starr's TiddlerToddler, it may even provide an interesting beginner's insight for those who are creating or updating documentation.

Why learn to program? Well, because it looks like fun. I've enjoyed working with developers for years now, and while I've dabbled in HTML from time to time, I haven't taken it any further than that. In terms of choosing a language to program in, I really wanted to get building things quickly, so I've plumped for Python, as a route towards using Django. I often hear Python and Django being referred to as a fun and quick route to build quality apps. So let's see how we go.

My starting point has been Alan Gould's Learning to Program guide. I should pause here and say, from my experience working with the TiddlyWiki community, that creating this kind of guide is incredibly time consuming and rarely gets the appreciation it deserves - and its free! So I'd like to start by putting my thanks and appreciation firmly on the record. Thanks, Alan!

It's been a great guide so far, but I came across two problems which I'd like to document here. I'm using Mac OS X (toggling between Leopard on my iMac and Snow Leopard on my Macbook Air), which has Python installed by default, but this isn't the most up to date version. I wanted the most recent version so that my new skills would be as relevant as possible, and the most stable release is v3.1.2 - so I went ahead and downloaded this (it's just a .dmg file, containing an .mpkg file - simple to install).

But for the life of me when I went back to the terminal, and typed "python", the old version kept coming up like this:


It launches Python 2.5.1. I even tried explicitly ticking the "Shell Profile Updater" option during installation - it made no difference. I even tried Google, but had little luck. At this stage I was getting hugely frustrated that I couldn't get over this first hurdle, so I sent out a message on Twitter and was amazed to get help within seconds from Victor Miclovich:



Victor, you are a legend - thank you! And you've inadvertently proved out my assumptions about how helpful the Python community is!

(Incidentally, I've found you don't need the dollar sign - just typing python3 works too)

I also encountered a second problem. Alan's tutorial presents the option between using the terminal (which my colleagues generally favour) or IDLE - an integrated developer environment, which came with Python. Well, the terminal wasn't really working out for me at that point, so I tried IDLE, and followed a link from Alan's tutorial to this IDLE help page.

But when I tried the "Hello world" example, I kept getting a syntax error as follows:


Fortunately, this one was easier to solve. It turns out that Python 3 requires a different syntax from Python 2. Instead of:
print "Hello world"

I needed to type:
print ("Hello world")
Success! (this shows the terminal again, though it worked in IDLE too)

OK....I'm easily pleased, I know!

So..this could be the start of an interesting journey. I can't help thinking about Nicole Lazarro's game theory talks at SXSW and Supernova last year. She talks about "hard fun" - the notion that people are willing to spend a certain amount of time being frustrated in return for the elation of a breakthrough (this applies to games such as Halo, too). So, let's see how long I can keep this up...

5 comments:

FND said...

Excellent choice, Phil!

However, I'd advise not to go with Python3 at this point, as it's not gonna be mainstream for a while. Among other things, this means that the availability of third-party packages is currently limited - a significant factor.
Python 2.6 (even 2.5 if necessary) is perfectly fine for your purposes, and will save you lots of trouble.
Transitioning to 3.x should be relatively easy once you got the basics down.

FWIW, you might look into Quickly and Acire for useful code snippets (while both projects are driven by Ubuntu, the sample code should be useful across platforms).

Also, Mark Pilgrim's Dive Into Python is a great resource - though I'm not sure about beginner-friendliness.

Phil Whitehouse said...

Cheers Fred - thought you'd approve of my choice!

Interesting advice about Python 3 though. I'm not sure how long it'll take me to learn Python and then Django, but I'm expecting months rather than weeks. It'll be a while before I'm using advanced features in anger. Given that the move towards Python 3 is pretty inevitable, is it reasonable to suggest that the most important third party packages will become available over the next few months in any event? The prospect of having to re-learn Python 3 after nailing Python 2 is pretty daunting...

In any event, I'll add Quickly and Acire to my list of things to learn - cheers!

FND said...

> Given that the move towards Python 3 is pretty inevitable, is it reasonable to
> suggest that the most important third party packages will become available
> over the next few months in any event?

I think it's gonna take years rather than months - that's just the nature of these things, and not necessarily a bad thing.

> The prospect of having to re-learn Python 3 after nailing Python 2 is pretty
> daunting...

There aren't really any massive differences which would warrant the term "re-learn", especially for casual coders like yourself. It's mostly minor changes like the different syntax for print, and there are migration tools which analyze your code and suggest changes.

Martin Budden said...

Phil,

great to see you're learning programming. I agree with Fred, Python is an excellent choice. I also agree with Fred - I'd avoid Python3 at this point, 2.6 is a better choice at this point, especially for a learner. As a learner you will need help and advice and that is most likely to come from people using 2.6 (Fred and myself included!). If you are using 3 and someone sends you a code snippet and it doesn't work you'll probably have to track down the differences between 2.6 and 3 to work out why it doesn't work.

In short, if you try and learn Python 3 what you will have to learn is Python 3 and Python 2.6 and the differences between them. A much harder task than learning Python 2.6 and then transitioning to Python 3 a few years down the line.

Sean said...

Hi Phil, I'm doing the same and wrestled with deciding between 2.6 or 3 and finally settled on 2.6 for two reasons: 1) my colleagues are coding in 2.6 right now, and 2) MoinMoin is still running on 2.x so any macros I develop will be in 2.x for it.

Another great resource that I found for learning Python is a Byte of Python by Swaroop CH. He provides versions of his tutorial for both 2.x and 3.x at http://www.swaroopch.com/notes/Python.