This is a little bit of additional work to get you started using IDLE, the Python integrated development environment. You will encounter two programs here:
You may have already noticed some inconsistency in how we write the names of programs, but there is meaning in that. We use a typewriter font for commands that you could type at the shell prompt, while we capitalize proper nouns more normally when naming the program. Thus, python is the command that implements the Python language, just as the IDLE program is invoked with the command idle. (And, no, I’m not completely sure why the creators of IDLE gave it an all-uppercase name, but they did.)
Because it is possible that you’ll end up using the CS lab computers for more than just this class, the first thing you should do is set up a directory (folder) to hold all of the work for this class. We’ll do more exercises soon to learn about working with directories, but for now, you should just follow these instructions.
Now you are ready to start up IDLE and begin working with Python. The command on cslab machines will be idle. A new window should appear, with the title “Python Shell”. Below a bit of initial verbiage, you should see the Python prompt:
Now it’s time to start typing in Python programs; the only drawback is that we don’t know very much Python yet…
The easiest thing to do is to type an expression or a statement at the prompt. Recall that an expression is evaluated to yield a value, while a statement is executed to have some effect. If you give the interpreter an expression, it will print out the value that results. Here are some examples:
The first and third lines are expressions; so the interpreter printed their values. The middle line of input is an assignment statement; it does something (make the variable thirteen have a value) instead of having a value itself. So the interpreter does not print anything after executing it.
If you use the arrow keys to position the cursor in a previously typed statement and then hit enter, IDLE will copy that statement to the prompt and give you a chance to edit it. This will also work if you position the cursor on a line of output.
Now add the following input:
This is another statement: print instructs the interpreter to write out the values of the expressions that follow.
For almost any interesting program, you will want to collect the Python statements in a separate file, so that you can later execute them whenever you wish. IDLE provides an easy way to do this.
You’ll see the results in the interpreter window (the one with the title “Python Shell”). But notice that you see only what you explicitly said to print. When you run from a file, the values of expressions are not printed: that behavior happens only when you are working at the interpreter prompt.
(If there was something wrong with your program, you’ll get one or more error messages. Go back to the editing window to fix any errors, the save and run again. If you typed perfectly, go back and mess up your input so that you can enjoy this experience.)
There is one more way to execute your program. Exit IDLE so that you are again looking at the shell prompt. Now type the command
Your program should run, with the same output you got running it within IDLE. (Later in the semester, we’ll learn how to fix our programs so that someone can use them without being aware that they are written in Python.)
Feel free to play around some more.
Be sure to exit IDLE and log out from your computer before you leave.
Check the class page at http://cs.wheaton.edu/~cgray/csci233/ for information about how to get Python (including IDLE) for your own computer running Microsoft Windows, Mac OS X, or Linux.
1If we are very strict with our language, the python command is not really an interpreter, but a translator. But does a very good job of faking it; so it we’ll talk about interacting with it as if it were an interpreter.