The intended audience for this document is the Fall 2010 ISTA 130 class at the University of Arizona. If other people also find it helpful, great!
IDLE is the integrated development environment (IDE) provided with Python. An IDE combines a program editor and a language environment as a convenience to the programmer. Using IDLE is not a requirement for using Python. There are many other IDEs that can be used to write Python programs, not to mention a variety of text-based programmer's editors that many programmers prefer to IDEs.
We are covering IDLE because it comes with Python, and because it is not too complex for beginning programmers to use effectively. You are welcome to use another editor or IDE if you wish, but if you don't already know one, IDLE is a good choice.
If you are reading this, you have probably already installed Python and IDLE. If not, now would be a good time to do so. We have a short web page with installation instructions: Installing Python.
With Python and IDLE installed, IDLE can be launched from your OS's program collection. For example, here's where you might find it if you using Ubuntu Linux with Gnome:
Clicking on the IDLE selection will launch IDLE and display the Python Shell window.
The top of IDLE's Python Shell window will look something like this:
This window serves two main purposes: To let us experiment with Python commands, and to let us open a program editing window.
Python, by design, is an interpreted language (as opposed to a compiled language). Programs written with interpreted languages do tend to execute a little more slowly than do compiled programs, but for most tasks the difference is insignificant. The big advantage of an interpreted language is experimentation: A programmer can try out algorithms and play around with different commands interactively (that is, without having to write a complete program to do either). For people just starting to learn programming in general, or Python in particular, interactive experimentation is very useful.
This interactivity is the main purpose of the IDLE's Python Shell window. You probably noticed already that the last line of text in the window is the set of three 'greater-than' symbols. This is the prompt symbol; when you see it on a line by itself, Python is waiting for you to do something -- such as type in a command.
Let's give it a try. Click on the Python Shell window (to make sure that it has the keyboard's attention) and then type this line, just like you see it:
print("Bear Down, Wildcats!")
As you are typing, you'll probably be distracted by a little pop-up message that shows the basic structure of an invocation of the print function. Just ignore it. You'll also notice that parts of the line you're typing appear in different colors. That's also not important right now.
When you're done typing, press the ENTER key. You should see the following in the window (minus all the colors):
>>> print("Bear Down, Wildcats!") Bear Down, Wildcats! >>>
Not terribly exciting, of course, but useful: If you were able to execute that print, you can execute all sort of other Python commands and see what they do. As an example of something more complex, let's get Python to display the odd integers from -9 through 9. Now, here's what we will be typing, but please don't do it yet:
>>> for i in range(-9,10,2): print(i, end=" ")
Notice that this example is on two lines, and that the second is indented by four spaces. You will have to tell IDLE to go to the second line (by pressing the ENTER key after you type the colon), but IDLE will take care of the indenting for you. When you get to the end of the second line, press the ENTER key twice. Got it? Great; go ahead and type it in. After pressing the ENTER key twice at the end, here's what you should see:
>>> for i in range(-9,10,2): print(i, end=" ") -9 -7 -5 -3 -1 1 3 5 7 9 >>>
The intentation is a big deal to Python; that's how it knows that the second line is actually a component of the for statement that was started in the first line.
That's all we want to do with experimentation for now. We're sure that you have several questions (e.g., "Why does the range go to 10 but the numbers stopped at 9?"), and that's good. We'll be covering the for statement in class in detail. In the meantime, you can consult the text for answers to such questions.
Playing around with Python commands in the shell window is nice, but our goal is to write complete Python programs. To do that, we need to start IDLE's editor. Here's how: From the Python Shell window, click on the File option on the menu bar, and select "New Window".
You will see a (surprise!) new window, one without a title. This is an editing window. Into it we can type sequences of Python commands to form programs. Unlike the shell window, we won't see the effect of the commands as we type them; we'll have to execute them as a group after we enter them to see what they do.
Into the new window, enter the following sequence of Python statements, just as you see them. Remember to press ENTER at the end of each line:
name = input("Please enter your name: ") print("Hello, " + name + "!") print("Your name has " + len(name) + " characters.")
Double-check that you typed the three lines exactly as you see them. Why is this a big deal? Because there is one intentional error in this program, and we're about to see how Python tells us about it. If you mistyped something, you may have created a second error, and if so the rest of this example won't go as we've planned.
Before IDLE will let Python execute this program, it will insist that we save the program as a file. This is a good idea; by saving it, we won't have to type it in again. Let's let IDLE nag us. In the menu bar, choose "Run", and from the drop-down menu choose "Run Module." Immediately, IDLE will pop up a little window that says, "Source Must Be Saved. OK to Save?" Click OK, and IDLE will show you a Save As window. Give it the name name.py and click "Save". Immediately, Python will start running your program; you'll have to look at the Python Shell window to interact with it.
In the shell window, you should see the "Please enter your name:" message. Go ahead and type in your name, followed by the ENTER key. The program will greet you, but then will encounter a problem. Here's what you should see in the Python Shell window (if your name is Rumpelstiltskin!):
Please enter your name: Rumpelstiltskin Hello, Rumpelstiltskin! Traceback (most recent call last): File "name.py", line 3, in
print("Your name has " + len(name) + " characters.") TypeError: Can't convert 'int' object to str implicitly >>>
When you see a "Traceback" message block in red, you know that there's a problem with your program. In this case, as we mentioned above, we inserted an error intentionally. The problem is that we are trying to create a message by concatenating (gluing together, if you will) three pieces of information. That's allowed, but only if the pieces are all collections of characters. Here, len(name) produces a number, not a collection of characters, and so Python is not able to concatenate it with the surrounding characters.
To fix this error, go back to the program window (which is now named "name.py") and change len(name) to str(len(name)) without changing anything else. Choose "Run" and "Run Module" again, and Python will ask that you approve the saving of the source. Click "OK", and enter your name in the shell window again. If you entered the program accurately and made the change correctly, here's the result of the program's execution:
Please enter your name: Rumpelstiltskin Hello, Rumpelstiltskin! Your name has 15 characters. >>>
One last thing: Go back to the "name.py" window and run the program one more time. Did you notice that IDLE didn't ask for the program to be saved? That's because you didn't change it since the last execution.
You now know enough to enter, save, and run Python programs using IDLE!
When you're done, to leave IDLE all you have to do is close the windows. Because IDLE is so insistent that you save your program before each execution, it's hard to lose your changes when you leave IDLE. But, if you want to be really safe, you can save your program manually before closing the editing window. Choose "File" on the menu bar, and "Save" from the drop-down menu.
Of course, this web page only touched on IDLE's capabilities. If you would like to learn more, here are some sources of additional information:
Do you have a comment on this page? I'd like to hear it; you can email me at email@example.com.