CSCI 233 Python Exercise 1

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:

python
is an interpreter for the Python language; you can think of it as creating a computer for which the instructions are written in Python (instead of bits).1
idle
is an integrated development environment, or IDE, for Python. IDLE allows us to create, read, and modify programs as well as execute them. It also includes tools for diagnosing and fixing incorrect programs, just in case you should ever make a mistake.

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.)

Setting up a working directory

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.

  1. Type the command cd. This command will always take you to your home directory. When simple Unix commands succeed, they usually do not print anything; so all you should see is a new prompt.
  2. Type the command pwd to print your current working directory. You should see something like /homestu/msnerd (if msnerd were your user name).
  3. Now do mkdir -p csci233/ex1 to create a directory for this exercise. (The command won’t print anything if it works).
  4. Now cd csci233/ex1 to go to the newly created directory.
  5. Verify that you are there with pwd.

Starting IDLE

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…

Expressions, statements and assignments

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:

>>> 3 + 2 * 5
13
>>> thirteen = 13
>>> thirteen
13
>>>

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:

>>> print thirteen, 13

This is another statement: print instructs the interpreter to write out the values of the expressions that follow.

Working with a file

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.

  1. Select “New window” from the “File” menu.
  2. Type your statements in that window (use here the same statements you did above):

    3 + 2 * 5
    thirteen = 13
    thirteen
    print thirteen, 13
  3. When you are done, select “File”, “Save as”, and provide the name “p1.py” for your file. You’ll want to save your Python programs in files with names that end in “.py”.
  4. From the “Run” menu, select “Run module”. IDLE will now execute your program.

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

python p1.py

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.

Getting your own copy of Python

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.