6.095: Introduction to Computer Science and Programming
Recitation 0: Python and IDLE

Introduction

This recitation will cover how to set up Python and introduce you to IDLE, the Python development environment we will be using throughout this course.

Setting up Python

On Athena / Linux

Python should be set up correctly on the Linux athena machines. Type 'idle' at the command prompt to ensure that everything is working correctly. This should start up the Python development environment IDLE.

On your own machine

If you are working on your own machine, you will probably need to install Python. We will be using the standard Python software, available here. You should download and install version 2.3 or later.

Windows:
Download and install: Windows Installer (Python 2.4.2)

Mac OS X:
In general, we have not tested Python on Apple machines, but we are not aware of any reason it should not work. OS X 10.3 (Panther) comes pre-installed with basic Python. You might need to install these add-ons. For OS X 10.2 (Jaguar), you can install MacPython 2.3 from here. See the MacPython download page for more help.

Using IDLE

IDLE is the standard Python development environment Its name is an acronym of "Integrated DeveLopment Environment". It works well on both Unix and Windows platforms.

It has a Python shell window, which gives you access to the Python interactive mode. It also has a file editor that lets you create and edit existing Python source files.

During the following discussion of IDLE's features, instead of passively reading along, you should start IDLE and try to replicate the screenshots.

Interactive Python shell

When you start up IDLE, a window with an interactive Python shell will pop up:

You can type Python code directly into this shell, at the '>>>' prompt. Whenever you enter a complete code fragment, it will be executed. For instance, typing:

>>> print "hello world"
 

and pressing ENTER, will cause the following to be displayed:

hello world
 

Multi-line expressions:
You can enter multi-line Python expressions at the prompt. For example, the following shows a function definition entered at the prompt:

For multi-line statements, you will have to enter a blank line and press ENTER, before the statement is executed.

Automatic indentation:
While you type, the editing environment will try to automatically indent your code. If you type in the above example you will find that when you hit ENTER after the def or while clauses, the next line is automatically indented for you (as you enter a new block). When within a block, you will be automatically placed at the same level as the previous statement (above, this happens for the num = num - 1 statement). Finally, each press of the BACKSPACE key at the start of an indented line will back you out of a level of indentation (above, it needs to be used before typing the return statement).

Colorization:
You may have noticed that your code is automatically colorized as you type, with different parts having different colors.

The way a piece of code is colorized depends on the Python syntax of that code. For instance, if a line is a comment, it is colored red. Python keywords are colored orange, strings are colored green, definitions are in blue, and the interpreter's normal output is also in blue.

Errors:
Interpretor output drawing your attention to an error will be in red. The above example shows the Python interpretor telling you there is something wrong with the following statement:

>>> factorial * 5
 

We are telling Python to 'multiply' a function by the number 5, which is not legal, the * operator cannot be used in this way.

The error message has two parts. The purpose of the Traceback part is to tell you where in your code the error occurred.

Traceback (most recent call last):
  File "<pyshell#10>", line 1, in -toplevel-
    factorial * 5
 

When the error occurrs in a file, you will see the filename instead of <pyshell#10>, and the line number where the error occurred. The traceback also shows the expression that generated the error.

The second part of the message is a description of the error.

TypeError: unsupported operand type(s) for *: 'function' and 'int'
 

Error messages are not always clear, they are often esoteric, but they are better than nothing.

Command history:
When you are working in the interactive shell, you can use the command history mechanism. This can often save you from having to do a great deal of repetitive typing.

You can use the ALT+P and ALT+N keys at the prompt to cycle through previously entered commands. Once you have found the command you want, you may edit it, before hitting ENTER at the end of the code, to execute it again.

You can also move the cursor (using the UP, DOWN, LEFT, and RIGHT keys) to move through the shell, to find the piece of code you want. Placing the cursor at the end of a line (e.g., factorial(5)) will bring that line down to the bottom. Placing the cursor at the end of any line in a compound statement (e.g., a def or a while) will bring a copy of all the lines in that compound statement to the bottom, for you to edit.

Pop-up help:
As soon as you type in the opening bracket for a function call a small box will pop up below the current line giving you information about that function. This box will give the names of the parameters for the function, and that function's documentation string. The window stays up until you enter the closing bracket.

This occurs for built-in functions, any functions from Python's library modules, as well as for any function defined by yourself. It does not, however, occur for list, tuple or dictionary methods.

File editor

While the interactive shell can be incredibly useful, you will probably spend most of your time in IDLE's file editor. You can start the editor by selecting File->New from the menu (or pressing CTRL+N). A window will pop up, and you can enter Python code into it. The code will be automatically indented, as before. However, you will need to save your code as a file with a .py extension (File->Save from the menu, or CTRL+S), before it is colorized. You can also open existing files in new editor windows by using File->Open (or CTRL+O).

The following shows an editor window for the file factorial.py:

You should explore the functionality provided by the commands in the Edit and Format menus of the editor. You might find some of these commands to be invaluable.

Running the code
You can execute the code in an editor window by using Run->Run_Module from the menu, or by pressing F5. The output of your program will appear in the interactive shell window.

Running factorial.py, shown above, prints out 120.

Once you have run a file, you can call functions defined in it directly from the interactive shell. However, any changes made in the editor will not be available in the shell, until you run the file again.

--------
The tutorial for IDLE is based on the official IDLE tutorial by Daryl Harms.

Asfandyar Qureshi, Feb 2006.