A.1 Downloading and Installing Python
Python is distributed under an
open source license
that makes it free to use, even for commercial products.
To download Python go to www.python.org
Download the current production version
for your operating system and CPU, for example
Save it in a folder of your choice or to Desktop.
Double-click on the saved
.msi file to run the installer.
Accept the defaults, only consider installing Python in
rather than in the suggested
The installation includes the Python interpreter, IDLE — a very simple
development environment with a graphical user interface (GUI),
libraries, tools, and documentation (a tutorial, a reference manual, etc.).
It is possible to run Python in a Command Prompt window,
but for most purposes it is more convenient to use IDLE,
rather than the “raw” command-line user interface.
To run IDLE, go to
Start/All Programs/Python 2.5 and choose
IDLE (Python GUI).
A.2 Configuring the Environment
It is better to create a shortcut to IDLE on the desktop.
To do that, go to
Start/All Programs/Python 2.5 and drag
IDLE (Python GUI) to the desktop,
while holding the
Ctrl key down, to create a copy (you should see a small
+ next to the icon).
You can also create a shortcut to the Python documentation:
drag and drop (with
As other programs, IDLE uses the concept of the working directory —
the folder where the interpreter looks for Python programs and data files.
By default, the working directory is set to the same folder where Python is installed.
It is not a good idea, though, to place your files into the installation folder.
Instead, reconfigure the shortcut you created to specify your own working directory.
To do that, right-click on the shortcut and choose
Start In line, enter your work folder, for example
(If your working directory name has spaces in it, put it in double quotes.)
Check your configuration:
- Double-click on the shortcut icon to start the interpreter
- At the prompt, enter:
>>> from os.path import abspath
\\ stands for one double slash.
abspath('.') returns the absolute path of the current directory.
It is possible to change the working directory from the interpreter itself.
>>> from os import chdir
By default, Windows operating systems hide file extensions.
When you are developing programs, it is more convenient to see the extensions.
To enable extensions, go to the
Tools menu in any folder, click
Folder Options.../View and uncheck the
Hide extensions for known file types box.
A.3 Running Python Programs
A short program can be typed directly into the interpreter:
>>> print 'Hello, World!'
>>> n = 1
>>> sum1toN = 0
>>> while n <= 5:
sum1toN += n
print "%3d: %5d" % (n, sum1toN)
n += 1
This is not very practical, though, because you have to reenter every statement to run the program
again or to make a small change to it.
In IDLE, you can click on one of the previous statements
(or move the cursor up to the appropriate line) and press
<Enter> to copy the
You can also edit the statement.
Still reentering every statement of a program would be too tedious.
It is more practical to save the program statements
in a file and execute the program from the file.
A file that contains the text of a program is called a source file.
Python source files usually have the extension
You could create a source file using any text editor,
for example Notepad.
You could even use a word processor — just make sure you save your file as a "text only" file,
and that you replace the default
.txt extension in the file name with
However, the easiest way to write a short Python program
is by using IDLE's own built-in editor.
To open a new editor window in IDLE, choose
New Window from the
Ctrl-N). Type in the text of your program.
The IDLE editor automatically increases indentation for statements
that expect it:
Backspace to decrease the indentation level.
Save As... from the
File menu or press
Ctrl-S to save the program in a file.
.py extension with the file name.
Save the file in a folder of your choice, for example,
While the IDLE editor window is open and active, you can test your program by choosing
Run Module from the
Run menu or simply by pressing
If your program has syntax errors, Python will alert you to that and highlight the first error.
(a semicolon instead of a colon).
You can have several editor windows open at once and cut and paste text
within the same window or from one window to another.
Highlight the text you want to copy, press
Ctrl-C to "cut" the text,
position the cursor at the insertion point, press
Ctrl-V to paste the text.
You can run a Python program from IDLE without opening it in an editor window
by calling the
assumes that the
MySums.py file resides in the Python’s working directory.
If your source file is in a different folder, you can pass its absolute or relative path
\\ for one backslash or simply use one forward slash as the separator in pathname strings.)
A.5 Where to Go from Here
The IDLE environment is adequate for working on programs in our Math and Python book.
Most of the programs are just a few lines of code, only one or two exceed half a page.
There are many professional development tools for Python programmers, of course.