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 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.
- MAC OSX Terminal Basics
- Basic Command Line Utilities, Tips and Commands
- Windows CMD commands
- Documentation for cmd.exe on Windows XP
Apart from the basic terminal commands, IDLE has a specific process that is used in order to run Python programs.
- Select: File > Open New Window
- Type in your basic code.
- Select: “Save as” and be sure to name your document with “.py,” as Python program names end with py.
- Select: “Run Module” and select “nameofpythoncode.py”
- 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!
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.