Dec 122011
 

This is the second in a series of preliminary posts about Python. Last time, we looked at why humanists would want to use Python. This time, we’re going to look at IDLE (a basic Python client) and the philosophy behind Python. A short note about the zen of python before we begin. The zen philosophy added to the Python code by Tim Peters was initially intended to be a joke. However, many programmers have noted that there are significant lessons to be learned by looking at the poem. In an interview with Bill Venners in 2003, Bruce Eckel said “One of the things I find that’s remarkable about Python is that it has a very even learning curve. Maybe it’s not even a curve, It’s kind of a straight line. Learning Python has a zen-like quality, because Python doesn’t try to make the world something else.” What does this mean? Python’s language is designed for simplicity. It builds upon the concepts people use everyday. This is one of the reasons why so many programmers suggest Python as a first programming language.

IDLE
IDLE stands for “integrated development environment” and acts as the shell interface for Python programming. Just as Python is said to have been named after Monty Python (yet another reason why humanists should program with the language), Mark Lutz and David Ascher report in Learning Python that IDLE was named after Monty Python actor Eric Idle. As a terminal or shell program, IDLE displays an interface much like older DOS systems.

Like DOS, IDLE requires some basic command codes to work. Now, many people who are faced with learning how to use a shell complain about not being able to simply rely upon a graphic interface. The shell interface is both quicker to use than Windows or other forms of OS and easier to modify when programming. Here are some great resources for beginning to use your shell and learning basic commands.

  1.  MAC OSX Terminal Basics 
  2. Basic Command Line Utilities, Tips and Commands
  3. Windows CMD commands
  4. Documentation for cmd.exe on Windows XP
IDLE is bundled with the newest version of Python, so you needn’t worry about getting both of them. Simply go to
http://python.org/download/releases/3.2.2/ and choose the Python version that works for you.

Using IDLE
Apart from the basic terminal commands, IDLE has a specific process that is used in order to run Python programs.

  1. Select: File > Open New Window
  2. Type in your basic code.
  3. Select: “Save as” and be sure to name your document with “.py,” as Python program names end with py.
  4. Select: “Run Module” and select “nameofpythoncode.py”
  5. Watch your Python code run!

The Zen of Python
Before we start learning Python, I’d like to meditate a bit on its philosophy. We talked last time about how Python is simple. That simplicity is built into the very language of Python and should be a guiding principle when you start working with code. The Zen of Python is a poem written by Tim Peters, included in the language, and intended to be humorous. But several programmers will tell you that the fundamental principles of the language can be found in the poem. You’ll get the following poem if you type “>>>import this” into the command line.

    Beautiful is better than ugly.
    Explicit is better than implicit.
    Simple is better than complex.
    Complex is better than complicated.
    Flat is better than nested.
    Sparse is better than dense.
    Readability counts.
    Special cases aren't special enough to break the rules.
    Although practicality beats purity.
    Errors should never pass silently.
    Unless explicitly silenced.
    In the face of ambiguity, refuse the temptation to guess.
    There should be one-- and preferably only one --obvious way to do it.
    Although that way may not be obvious at first unless you're Dutch.
    Now is better than never.
    Although never is often better than *right* now.
    If the implementation is hard to explain, it's a bad idea.
    If the implementation is easy to explain, it may be a good idea.
    Namespaces are one honking great idea -- let's do more of those!
Let’s unpack some of the ideas in this poem. NB: Most of these programs are adapted from Paul Berry and David Griffiths’s Head First Programming. 
1. Beautiful is better than ugly: Python is written to be easier for human beings to read, with simple and consistent syntax. For example, to print “Hello World,” the first command most Python users learn, you simply write:
print '"Hello World!"

2. Explicit is better than implicit: Python is based upon a Boolean type, which means that it explicitly and easily processes true/false statements. In the following example, where we create a short program to determine if an input = 55, true/false is expressed by if/else. Also, note how easy it is to read the code:

print("Welcome!")
g = input("I'm thinking of a number between 1-100. What's the number?:")
guess = int(g)
if guess == 55:
   print("Yes!")
else:
   print("No!")
print("Hope You Had Fun!")

The same kind of explicit philosophy is embedded in the way Python parses loops. Loops are used in programming to allow users to continue to provide answers until they get the correct one. For example, here is a loop program that continues to ask if you are my cat as long as you answer no:

answer = "no"
while answer == "no":
   answer = input("Are you Roger's cat Nemo?")
print("correct!")

In each, the loop and the if/else statements are written separately and explicitly from the rest of the code. This is the way that Python keeps its code human-readable and, in a word, elegant. Much of the time, you can scan Python code and (if you understand the basic underlying syntax) get a sense of what the code does.

In my next post, I’ll continue an introduction to Python through the Zen poem, and give you some Python programs which can directly benefit humanities teachers. I will also present you with the first in a series of homework assignments! Yay! By the way, I’m using these posts to learn Python myself, so I am sure that I will be slightly off or just plain wrong from time to time. I would appreciate any suggestions that would help my code. Further, if you have suggestions for posts and topics, I’d love to hear them.

  • Andrew Famiglietti

    It’s fun to watch you learn Python, Roger! As a stretched-in-too-many-directions digital humanist, I often find myself returning to Python after long breaks of not doing any coding. Python’s simplicity is great then, though it will often make you feel like an idiot. For example, you might spend 10 minutes wracking your brain trying to remember how to get elements out of a dictionary object in some kind of sane order, only then to remember that you can sort a dictionary simply by using the .sorted method. You feel a bit like the “Flowers for Algeron” kid then :)

    • Roger Whitson

      I can’t WAIT for that. ;)

  • http://jgoodwin.net Jonathan

    We used to teach perl at Florida. This saddens me somewhat.

    • Roger Whitson

      The post or the fact that UF used to teach Pearl? I don’t know Pearl well enough to be happy or sad.

      • http://jgoodwin.net Jonathan

        I assume that “pearl” is an autocorrect artifact. Perl lends itself, like poststructuralism, to obscure, write-only code; but it’s very powerful. Bradley Dilger did actually teach a perl course at Florida, I think, though I only remember a few of us using it for things then.

  • Pingback: Teaching Carnival 5.05 - ProfHacker - The Chronicle of Higher Education

  • Pingback: Teaching Carnival 5.05 (in progress!) | Cerisia Cerosia