Math and Python Home Page Skylight Publishing

Mathematics for the Digital Age
Programming in Python

Appendix A: Getting Started with Python


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 and click Download.  Download the current production version for your operating system and CPU, for example python-2.5.1.msi.  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 C:\Program Files\Python25 rather than in the suggested C:\Python25.  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 Ctrl down) Python Manuals.

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 Properties.  In the Start In line, enter your work folder, for example C:\mywork

Figure 1

(If your working directory name has spaces in it, put it in double quotes.)

Check your configuration:

  1. Double-click on the shortcut icon to start the interpreter
  2. At the prompt, enter: >>> from os.path import abspath >>> abspath('.') Python responds: 'C:\\mywork'
Here \\ 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.  For example:

>>> from os import chdir >>> chdir("C:/myOtherWork")

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!' Hello, World! Or: >>> n = 1 >>> sum1toN = 0 >>> while n <= 5: sum1toN += n print "%3d: %5d" % (n, sum1toN) n += 1 1: 1 2: 3 3: 6 4: 10 5: 15 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 statement.  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 .py

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 .py.  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 File menu (or press Ctrl-N).  Type in the text of your program.  For example:

Figure 2

The IDLE editor automatically increases indentation for statements that expect it: def, while, if, etc.  Press Backspace to decrease the indentation level.  Choose Save As... from the File menu or press Ctrl-S to save the program in a file.  Use the .py extension with the file name.  Save the file in a folder of your choice, for example, C:\mywork.

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 F5. If your program has syntax errors, Python will alert you to that and highlight the first error.  For example:

Figure 3

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

A.4   Using execfile

You can run a Python program from IDLE without opening it in an editor window by calling the execfile function.  For example:

Figure 4

The statement

execfile("") assumes that the 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 to the execfile function.  For example: >>> execfile("C:/mywork/sums/") or >>> execfile("sums/") (Use \\ 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.  See

Copyright © 2008 by Skylight Publishing