On the shortest Python program

[First published in GOTOXO as Python-XO: On the shortest Python program on December 19, 2007.]

As reported in Caduceus Redux, Robert Bolgar says, “Maybe I should gear up my brain to learn some Python, too. About time I woke up to the computing world.”

I write this note to Josh and Zander Bolgar to provide a suggestion on how to help Dad wake up to the world of computing. Other members of the XO Generation may find it a way to help start their parent’s education on programming as well.

One of the most common sources of error in writing in a program is the failure to properly deal with what are known as “edge cases.” For example, running a loop once too often, or failing to run it far enough.

A further example is whether the program properly deals with the “null,” or empty case.

Since Python is based on set theory, let’s see if the Python compiler handles the case of null input properly. Linux provides many useful features, among them a perpetual source of nothingness in the form of a special device known as “/dev/null”. It appears as a file, but whenever you try to read it you get only the end-of-file signal. There is no data in that file save its end.

The question then is what happens when you execute:

$ python shortest and longest sentences in natural language, in which a letter consisting of a single character (“!”) communicated a meaningful message. But Python is even better in that you can write a correct program by writing a program with no characters at all!

Zander and Josh can thus start their dad on his programming career by just having him execute the empty Python program.

If is of course much more fun to write programs that are longer and do some useful work, as I will try to show in future posts in this series. But education in any subject, including programming, must begin with a first step, even if it is just to show that “nothing ventured can still some education gain.”


  1. Posted December 19, 2007 at 21:55 | Permalink | Reply

    very interesting, i love concise programs–they are a thing of beauty.

  2. paddy3118
    Posted December 21, 2007 at 12:59 | Permalink | Reply

    python -c ”

    Tells the interpreter to interpret a command which is the null string, wheras you are telling python to start its command line shell and use /dev/null as input. They both ‘do the right thing’. Are equally pointless, (But mines shorter 😉

    – Paddy.

One Trackback

  1. By Laughable Links « The Wayward Word Press on December 20, 2007 at 10:57

    […] just noted that a link to my post On the shortest Python program can be found at Affordable Life […]

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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

  • Pages

  • December 2007
    M T W T F S S
  • RSS The Wayward Word Press

  • Recent Comments

    daveshields on SPITBOL for OSX is now av…
    Russ Urquhart on SPITBOL for OSX is now av…
    Sahana’s Respo… on A brief history of Sahana by S…
    Sahana’s Respo… on A brief history of Sahana by S…
    James Murray on On being the maintainer, sole…
  • Archives

  • Blog Stats

  • Top Posts

  • Top Rated

  • Recent Posts

  • Archives

  • Top Rated

  • %d bloggers like this: