A.5 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("{0:3d}: {1:5d}".format(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 copy one of the previous statements:
click on it or move the cursor up to the appropriate line and press <Enter>.
You can then 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:
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 an 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:
(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.6 Using exec
You can run a Python program from IDLE shell, without opening it in an editor window,
by calling the exec function.
For example:
The statement
exec(open("MySums.py").read())
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
to the exec function.
For example:
>>> exec(open("C:/mywork/sums/MySums.py").read())
or
>>> exec(open("sums/MySums.py").read())
(Use \\ for one backslash or simply use one forward
slash as the separator in pathname strings.)
A.7 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 http://wiki.python.org/moin/DevelopmentTools.