Introducing the Python IDLE


Python comes with a GUI-integrated development environment called IDLE. A development environment is a set of tools that makes writing programs easier. You can think of it as a word processor for your programs. But it's even more than a place to write, save, and edit your work. IDLE provides two modes in which to work: an interactive mode and a script mode.

HINT

MacPython has its own integrated development environment called IDE. It works a little differently than IDLE, but allows you to do the same basic things.

Programming in Interactive Mode

Finally, it's time to get your hands dirty with some actual Python programming. The quickest way is to start Python in interactive mode. In this mode, you can tell Python what to do and it'll do it immediately.

Writing Your First Program

To begin your interactive session, from the Start menu, choose Programs, Python 2.2, IDLE (Python GUI). You should see something very similar to Figure 1.4 on your screen.

click to expand
Figure 1.4: Python in an interactive session, awaiting your command.

TRAP

If you have an trouble running IDLE, you may need to modify your Windows System Path—a list of the directories where your computer looks to find program files. You'll want to add the following to the end of your current Path: ;c:\Python22;c:\Program Files\Tcl;c:\Program Files\Tcl\bin. The process of modifying your Path is different for each version of Windows, so check your Windows Help documentation for Environment Variable (since the Path is one of your Environment Variables).

This window, also called the Python Shell, may look a little different from the screen shot in Figure 1.4. At the command prompt (>>>), type: print "Game Over". The interpreter responds by displaying

 Game Over 

on the screen. Ta da! You've written your first Python program! You're a real programmer (with a little more to learn, but that goes for all of us).

Using the print Statement

Take a look again at the line you entered, print "Game Over". Notice how straight-forward it is. Without knowing anything about programming, you could have probably guessed what it does. That's Python in a nutshell. It's concise and clear. You'll appreciate this even more as you learn how to do more complex things.

The print statement displays whatever text you type between the pair of quotes. You can also use it by itself to print a blank line.

Learning the Jargon

Okay, time to learn some jargon. Now that you're a programmer, you have to throw around those fancy terms that only programmers understand. The line you entered in the interpreter is considered a statement. In English, a statement is a complete thought. In Python, a statement is a complete instruction. It does something. So, print "Game Over" is a statement.

The statement you entered is made up of two parts. The first part, print, is a command. It's like a verb. It tells the computer to take an action. In this case, it tells the computer to display text on the screen. Python is case-sensitive and commands are in lowercase. So, print "Game Over" will work, but Print "Game Over" and PRINT "Game Over" won't.

The second part of the statement, "Game Over", is an expression. It doesn't do something. It is something. A good way to think about it is that an expression has a value, like the letters in the phrase "Game Over", or even the number 17. An expression can also evalute to some value. For example, 2 + 5 is an expression that evalutes to 7.

In this particular case, you can be even more specific by saying that "Game Over" is a string expression. This just means that it's a series of characters, like the ones on your keyboard. "String" may seem like an odd name—"text" or "words" might be more clear—but the name comes from the idea that text is a string or a series of characters. (Not only do you know jargon, but you have some trivia under your belt now too.)

Now that you're a programmer, you can tell someone that you wrote some Python code. Code means programming statements. You can use it as a verb, too; you can say that you were up all night eating Doritos, drinking Jolt Cola, and coding like crazy.

Generating an Error

Computer's take everything literally. If you misspell a command by even just one letter, the computer will have absolutely no idea what you mean. For example, at the interactive prompt I typed primt "Game Over". The interpret responded with

 SyntaxError: invalid syntax 

Translated to English, the interpreter is saying "Huh?!" It doesn't understand primt. As a human being, you can ignore my typo and know what I meant. Computers are not so forgiving. This error in my statement, called a bug in a program, gets me an error message and nothing else printed on the screen. Specifically, this is a syntax error, meaning the computer doesn't recognize something. Syntax errors are usually just caused by a typo and are an easy fix.

Understanding Color Coding

You probably noticed that words on the screen are printed in different colors. This color coding helps you quickly understand what you've typed by visually categorizing it. And there is a method to this coloring madness. Special words, like print, are displayed in orange. Strings, like "Game Over", are in green. And the output of your statements—what the interpreter prints as a result of what you type—is in blue. As your write larger programs, this color scheme will come in really handy. It will help you take in your code in one glance.

Programming in Script Mode

Using the interactive mode gives you immediate feedback. This is great because you can see the results of a statement right away. But it's not designed to create programs you can save and run later. Luckily, Python's IDLE also offers a script mode, in which you can write, edit, load, and save your programs. It's like a word processor for your code. In fact, you can perform such familiar tasks as find and replace, and cut and paste.

Writing Your First Program (Again)

You can open a script mode window from the interactive window you've been using. Select the File menu, then New Window. A new window will appear that looks just like the one in Figure 1.5.

click to expand
Figure 1.5: Your blank canvas awaits. Python is ready for you to write a program in script mode.

Now type print "Game Over" and press Enter. Nothing happens! That's because you're in script mode. What you're doing is writing a list of statements for the computer to execute later. Once you save your program, you can run it.

Saving and Running Your Program

To save your program, select File, Save As. I gave my copy the name game_over.py. To make it easy to get to later, I saved it on my desktop.

To run my Game Over program, I simply select Edit, Run Script. ("Script," by the way, is just another name for a program.) Then, the interactive window becomes my active window and displays the results of my program. Take a look at my desktop in Figure 1.6.

click to expand
Figure 1.6: The results of running the Game Over program through IDLE.

You'll notice that the interactive window contains the old text from before. It still has the statement I entered while in interactive mode, print "Game Over", and the results, the message Game Over. Below all of that, you'll see the results of running the program from script mode: the message Game Over.

TRAP

To run your program from IDLE like I just did, you need to first save your program. If you don't, IDLE will give you a Not Saved dialog box.

TRICK

Interactive mode is great for trying out a small idea quickly. Script mode is perfect for writing programs you can run later. Using both modes together is a great way to code.

Even though I need only script mode to write a program, I always keep an interactive window open while I code. As I write my programs in script mode, I jump over to the interactive window to try out an idea or to be sure I have the usage of a command just right.

The script window is where I craft my final product. The interactive window is like a scratch pad where I can think and experiment. Using them together helps me to write better programs more quickly.




Python Programming for the Absolute Beginner
Python Programming for the Absolute Beginner, 3rd Edition
ISBN: 1435455002
EAN: 2147483647
Year: 2003
Pages: 194

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net