This page is meant to help new users of Python who might feel a little disoriented. One question that might come to mind is: ok, we've installed Python... ummm... now what?
It might be nice to have a "visual" guide to reduce any initial anxieties. That's what this page is for. The plan is to go through a small session with IDLE: the Integrated Development Environment. IDLE is designed to provide a simple way of exploring the language. During this session, I'll make a few fumbling mistakes, just to show what to expect when things don't go exactly hunky-dory.
By the way, this is an online document If you have any suggestions, or if you want to make corrections or improvements, please feel free to email at firstname.lastname@example.org. Small plug: also, don't forget that there's a great resource in the Python Tutor mailing list: we're a group of people who like to show each other interesting stuff in Python. We're all learning together, so feel free to subscribe and join us.
Ok, let's assume that we've already installed Python. (If not, we can visit: http://python.org and download the most recent Python interpreter. As of this writing, that's Python 2.1.) The first thing we'd like to do is actually start running it! We can do this by opening up IDLE, which should be in our Start Menu under the newly-created Python program group.
We'll see that a new window magnificantly opens up:
This is the main window to IDLE, and what we see right now is called the "Interpreter" window. The Interpreter allows us to enter commands directly into Python, and as soon as we enter in a command, Python will execute it and spit out its result back to us. We'll be using this Interpreter window a lot when we're exploring Python: it's very nice because we get back our results immediately. If it helps, we can think of it as a very powerful calculator.
Let's try something now! As per tradition, let's get Python to say the immortal words, "Hello World".
Those '>>>' signs act as a prompt for us: Python is ready to read in a new command by giving us that visual cue. Also, we notice that as we enter in commands, Python will give us its output immediately.
Ok, this seems pretty simple enough. Let's try a few more commands. If we look below:
we'll see the result of running a few more commands. Don't worry too much about knowing the exact rules for making programs yet: the idea is that we can experiment with Python by typing in commands. If things don't work, then we can correct the mistake, and try it again.
If you got to this point, you now know enough to start playing around with Python! Crack open one of the tutorials from the Python For Beginners web page, and start exploring with the interpreter. No time limit here. *grin*
Now that we've paddled long enough, we might be asking: ok, this is neat, but if we close down Python and start it up again, how do we get the computer to remember what we typed?
The solution is a little subtle: we can't directly save what's on the interpreter window, because it will include both our commands and the system's responses. What we'd like is to make a prepared file, with just our own commands, and to be able to save that file as a document. When we're in the mood, we can later open that file and "run" Python over it, saving us the time of retyping the whole thing over again.
Let's try this. First, let's start with a clean slate by opening up a new window.
Here's the result of that menu command:
We notice that there's nothing in this new window. What this means is that this file is purely for our commands: Python won't interject with its own responses as we enter the program, that is, not until we tell it to. I'll call this the "Program" window, to distinguish it from the Interpreter window.
What we wanted to do before was save some of the stuff we had tried out on the interpreter window. Let's do that by typing (or copy/pasting) those commands into our Program window.
Ok, we're done with copying and pasting. One big thing to notice is that we're careful to get rid of the ">>>" prompts because there's not really part of our program. The interpreter uses them just to tell us that we're in the interpreter, but now that we're editing in a separate file, we can remove the artifacts that the interpreter introduces.
Let's save the file now. The Save command is located under the File menu:
Now that we've saved the program, how do we run the program? If we look at the menus on our program window,
we'll see that there's a menu option to "Run script", and that's what we'll do. What we want to see is Python running through the program, and displaying its results in the Interpreter window.
By the way, one thing to notice is that I made a typo: I didn't quite copy exactly what I had entered in the interpreter window before. Does this affect things?
Ooops. Here is an example of what Python calls a "syntax error". Python sees that we made a typo, and warns us to take a much closer look at our program. The designers of Python feel that having the system point out the error is better than trying to guess at what the programmer meant. It's the idea of explicitness versus implicitness. There are certain rules that Python follows that measure what looks good and what looks suspicious. As we speak the language, we'll get a feel for these rules. And if you're feeling suspicious, yes, this is a bit like grammar. *grin*
Python is often perceptive enough to direct us toward the problem, and in this case, it's telling us that we forgot to put something at the end of this line. In this case, we need to add an additional quotation mark. Let's add that in now.
Ok, let's say that we fixed that silly typo. Let's try to run the program again.
Another snag, but not that complicated, just silly. One other thing that might put us off guard is that IDLE wants us to save any Program windows before we run them; this is a user interface issue that makes sure that we save our work before we start running the program.
(Note: the comment above might be outdated; enough people got annoyed by IDLE's lazy behavior that they've clamored for change. We hope that the development version of IDLE doesn't give this error message anymore.)
They say that third time's the charm, so let's try running it again. Hopefully, it should run well now.
As we play with Python, we'll find ourselves "switching modes" between the Interpreter window and the Program window. The reason for this is because we can use the Interpreter as if it were a small laboratory and experimentally try small programs. After we're satisfied (or when we're tired), we can save what we've learned into a program file.
This, of course, assumes that we can actually load the file later on; it would be a silly thing to save a program, and not be able to load it up later. Let's show this, and then stop for today. I'll close everything down in IDLE, and start from a clean slate.
We'll find the Open command under the File menu:
and if all goes well, we should see a new Program window open up:
with our old program. We're in business! We can save our old work and open it up at a later time. It's not jaw-opening, but it is crucial for the person who wants to play with Python for more than a day. *grin*
This is pretty much all we need to know about IDLE to actually do interesting things. This guide has skipped a lot of stuff about IDLE: IDLE is much more than an mere editor, but it takes some time to explore all of its features, so let's stop for now. There's a IDLE Documentation page that explains advanced IDLE use, for those with an insatiable curiosity. Again, if you have any questions, please feel free to stop by the Python Tutor mailing list; there's usually someone there who'd be happy to chat about Python with you. It's been fun, and I hope this has been helpful!
Back to my personal Python page.