Getting Started with Python in IDLE
Michael A. CovingtonLast revised 2004 March 24
Artificial Intelligence Center
The University of Georgia
IDLE ("Interactive DeveLopment Environment") is an older-style environment for developing Python programs ("scripts") under Windows and other operating systems.
Unlike the Pythonwin IDE, which you may also have used, IDLE runs a program very much the way it would run in a non-windowed environment. For example, raw_input() does not make a box pop up; it just pauses and waits for you to type something.
Interactive - You can interact with IDLE; you do something, the computer responds to it, you do something else, the computer responds to it, and so on. You do not simply put in your entire program and press one button.
Development - It's for developing Python programs.
Environment - It's an environment for you to work in. That is, a variety of tools for editing, checking, and running programs are all provided together.
Obtaining IDLEIDLE comes with the version of Python distributed by www.python.org and can (at present) be downloaded from www.python.org/ftp/python/2.3.3/Python-2.3.3.exe.
Simply download Python-2.3.3.exe and run it. You do not have to remove ActiveState Python if you also have it; you can have both on your computer at the same time.
Starting IDLETo start IDLE, pick it from under Python 2.3 in your Start Menu:
When it starts up, you'll see its main window, the "Python Shell":
What is this warning about firewall software?You can type Python statements and expressions for immediate execution or evaluation just the way your textbook indicates:
Because IDLE was originally developed under UNIX, its various components (such as multiple windows running at the same time) sometimes communicate as if they were using a network. Even though everything is on a single computer, some security software will notice the abnormal activity and complain about it.
Notice the color code. The colors can be very helpful in helping you recognize when you have misspelled a keyword or left out a quotation mark.Terminology:
Shell - The outermost layer of an operating system or piece of software that communicates interactively with the user. Originally, the "shell" was the part of UNIX or DOS that accepted commands typed by the user. Similar components of other software packages are sometimes called shells. The term is not widely used.
Expression - Something that describes a computation that gives a value. For example, 2+3 is an expression that can be evaluated to give 5, and (2+3)*4 is an expression with another expression inside it.
Evaluate - To determine the value of an expression.
Statement - Something that describes an action to be performed by the computer. For example, print "Hello" is a statement that tells the Python system to output Hello. (We usually talk about "statements" in programming languages and "commands" to an operating system, but there is really no difference.)
Execute - To do what a statement, command, or program says to do.
Writing a program (a script)Python programs are called scripts because they are executed immediately, one line at a time. The computer does not translate them into something else first. (Contrast this with the way C programs are translated into .exe files and then the .exe files are executed.)
To write a program, choose File, New Window. An editing window pops up:
Type your program in the editing window, and save it (File, Save As...).
Caution: You must give the script a name ending in .py - this is not done automatically.
Hint for the perplexed:
If Windows does not show you the .py ending, or any of the other endings on your file names, then:
- Open any folder.
- Under Tools, Folder Options, View, uncheck "Hide file extensions...".
Then you'll be able to see the endings of file names as you browse.
Note that as soon as you have saved the file with a .py ending, its contents become color-coded just like the things you typed in the Python shell. That's because the editing window has just now realized you're typing a Python program.
Running your programTo run your program, choose Run, Run Module, or just press F5:
When you do this, the Python shell window will come to the front and your program will run in it:
Modifying your program and running it againNow go back to the editing window, make a small change in your program, and hit F5 again. You'll get this cryptic message:
Source must be saved means "Your program must be saved." It is called source code because it is code written by a human being, not code generated by a computer. All computer programs, as written by humans, are called source code. Since Python programs are not normally translated into anything else before execution, the term "source code" is rarely used with Python. The astute reader will therefore realize that this editing window was originally used for languages other than Python, and it's giving away its origin.
As soon as you let IDLE save your program, it runs again in the Python shell:
Note that every time you start afresh, ====RESTART==== appears in the shell window. This helps you distinguish the latest run of your program from the previous one.
If you entered IDLE by right-clicking on a program and choosing "Edit with IDLE," then these ====RESTART==== messages do not appear; IDLE is running in a slightly different mode. But everything still works.
Programs with ErrorsNormally, if a program runs into an error while executing, you'll get a message about it in the Python shell window:
However, some kinds of serious syntax errors will prevent the program from even starting. In that case, the editing window cannot hand the program over to the Python shell, so instead, it will show you where the error was detected, like this:
This is much better than Pythonwin, which displays a small message at the bottom of the screen and otherwise seemingly does nothing.
Programs that consist of defsYou can perfectly well write a script that consists of nothing but defs (function definitions), then call the functions by typing expressions in the Python shell.
Running programs outside IDLEOne way to run a Python program outside IDLE is to simply right-click on it (in a folder or on the desktop):
(Python must be installed on your computer, of course.)
The program will then run in what is called a console window:
Terminology:Hint: Programs that run in a console window should generally end with something like
Console - the place from which an (old-fashioned, non-windowed) computer is controlled; its screen and keyboard.
Console window - a window that simulates an old-style computer screen on which only type can appear, no graphics.
to make the window remain visible until the user is finished with it. The window will disappear as soon as the program ends!
nor are they endorsed by, the University of Georgia or the University System of Georgia.