How to Python a puzzle

14 Apr

How to Puzzle a Python, by Max Walker

Any Australian fan of second-hand and remaindered books will have come across numerous copies of a book by Max Walker, the Australian cricket team’s long-standing wicketkeeper, with titles like ‘How to Hypnotise Chooks’, ‘How to Tame Lions’, ‘How to Kiss a Crocodile’ and ‘How to Puzzle a Python’. But the Python I have in mind is rather different.

The Python programming language is named after the Monty Python TV series, although the developers have been fairly restrained in their enthusiasm for the show: one popular development application is called ‘Eric’, and another is ‘Idle’, but there is little else about the language to indicate its origins. It was developed by Guido Van Rossum in the Netherlands during the 1990s, and started to achieve widespread use by the mid-2000s. As a free and open source language it is particularly popular among Linux users and enthusiasts. Many of the more user-friendly interactive programs found on Linux systems are written wholly or partially in Python.

My own history with programming languages is like the popular description of country music — a mile wide and an inch deep. In the last three decades I have dabbled in BASIC, Algol, Pascal, Visual Basic, C++, PHP, Bash scripting, Forth, OpenOffice macros, and ASP. I have even toyed with LISP and Prolog, though my reaction to those was the same as that of the majority of computer boffins: “Huh?” But from my own peculiar and amateurish point of view, Python is one of the best — if not the best — programming languages around.

Why so? Well, here’s a Python program that reads in a text file and outputs the whole thing in reverse:

#!/usr/bin/env python3.1
import sys
file = open(sys.argv[1])
data =

Five lines! and two of those are standard administrative entries. The closest program I could find in C++ requires 37 lines to do the same thing.

That’s the simple user-friendly version. Here’s the same program in three lines:

#!/usr/bin/env python3.1
import sys

Why is Python so smart? Because like Linux Mint, someone — presumably Van Rossum — has sat down and thought deeply about what a programming language is supposed to be and to do. And the developers have made a considered choice: put ease of use first, and then worry about complexity and power later. This is a complete reversal of the approach taken by Microsoft with Visual Basic, for instance, where they started with an easy-to-use friendly program and proceeded to add layer upon layer of complications until it became nearly as intimidating as C++. Obviously there are some programmers who base their self-esteem on the complexity of the stuff they have to write, but Python developers and users are not among them.

Among the brilliant decisions made in designing Python were:

  • Programmers should indent self-contained blocks. If they’re indented they don’t need start and finish markers. So Python eliminates those and uses indentation to mark the start and finish of blocks. Two less keywords or characters for everyone to try and remember.
  • Don’t require the program to work out what Python can look up for itself. Python knows, for instance, the length of the strings it is working with, so I can pull off the last character of a string with ‘string[-1]‘ without having to write code to look up the length first.
  • Allow the same data to be treated in multiple ways without explicit translation: so a text file can be treated as one big string of text, a series of lines of text, an array of lines of text, an array of arrays of words, an array of sets of words, and so on, just by using slightly different syntax.
  • Use the same functions and syntax sensibly on different types of data: so the [-1] method mentioned above can also be used to retrieve the last entry from an array, the last word from a set, the last item from a set, the last letter in a word, and so on and so forth. All the programmer has to remember is that [-1] ‘means’ last.

Python is particularly good at text manipulation, which is what I find I spend most of my programming effort on, so it has already done a lot to make my life easier.

The current version of Python is 3.1. It is available in the Linux Mint and Ubuntu repositories, and well documented online. There are a variety of programming environments available, including the aforementioned but for the simple stuff I do, I find that gedit is perfectly adequate. If you program even occasionally, and you haven’t tried Python yet, then you ought to.


Posted by on April 14, 2011 in Linux Applications, Python


2 responses to “How to Python a puzzle

  1. Joshua

    February 22, 2012 at 2:34 am

    Just for the record, Max Walker was a medium pace bowler for Australia, not the wicket keeper. I guess he looks a bit like Rod Marsh, they all had mustaches back in the 1970s.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: