Python for Humanists 2: Getting Started and the Zen of Python

Posted by Roger Whitson on December 12th, 2011

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.

Review of The Emergence of the Digital Humanities

Posted by Roger Whitson on August 28th, 2014

A New Birth of Academic Freedom

Posted by Roger Whitson on August 7th, 2014

My NASSR 2014 Presentation – “@autoblake: Between Critical Making and Computation”

Posted by Roger Whitson on July 8th, 2014

DHSI 2014 Day 1, or Why We Need the MLA Report

Posted by Roger Whitson on June 3rd, 2014

Some #dhsi2014 Essentials

Posted by Roger Whitson on May 25th, 2014

Faculty Letter about Sexual Assault on WSU Campus

Posted by Roger Whitson on May 15th, 2014

Reviewing the Reviewer: Grant Scott’s Review of Blake 2.0: William Blake in Twentieth-Century Art, Music, and Culture.

Posted by Roger Whitson on May 2nd, 2014

On Several Scales of Reals and Realities, Archival or Digital or Otherwise

Posted by Roger Whitson on April 18th, 2014