Running the Application
Applications aren’t much good if you can’t run them. Python provides a variety of methods for running any application you create. This section explores the easiest method for running an application after you create it. You see additional methods in the “Loading and Running Existing Applications” section of the chapter. The important thing to remember is that Python provides an extremely flexible environment, so if one method of performing a task doesn’t quite work, another method will almost cer- tainly succeed.
To run this first application, choose Run➪Run Module. You see a new copy of the Python Shell window opens and then the output of your application appears, as shown in Figure 4-16.
Figure 4-16:
The output
of the example application appears in a Python Shell
window.
The top two lines of the output in Figure 4-16 should be familiar by now — they’re the information that always appears when you start the shell. Next comes a
message. You see this message every time you run the application. To see this for yourself, select the Edit window and choose Run➪Run Module. The original Python Shell window is selected, another message appears, and you see the output from your application again, as shown in Figure 4-17.
Figure 4-17:
The Python
Shell window displays a Restart message each time
you run the application.
Understanding the Use of Indentation
As you work through the examples in this book, you see that certain lines are indented. In fact, the examples also provide a fair amount of white space (such as extra lines between lines of code). Python ignores any indentation in your application. The main reason to add indentation is to provide visual cues about your code. In the same way that indentation is used for book outlines, indentation in code shows the relationships between various code elements.
The various uses of indentation will become more familiar as you work your way through the examples in the book. However, it’s important to know at the outset why indentation is used and how it gets put in place. So, it’s time for another example. The following steps help you create a new example that uses indentation to make the relationship between application elements a lot more apparent and easier to figure out later.
- Choose File➪New
IDLE creates a new Edit window for you.
- Type print(“This is a really long line of text that will ” +.
You see the text displayed normally onscreen, just as you expect. The plus sign (+) tells Python that there is additional text to display. Adding text from multiple lines together into a single long piece of text is called concatenation. You learn more about using this feature later in the book, so you don’t need to worry about it now.
3es. sPErnter.
The insertion point doesn’t go back to the beginning of the line, as you might expect. Instead, it ends up directly under the first double quote,
as shown in Figure 4-18. This feature is called automatic indention and it’s one of the features that differentiates a regular text editor from one designed to write code.
- Type “appear on multiple lines in the source code ”) and press Enter.
Notice that the insertion point goes back to the beginning of the line. When IDLE senses that you have reached the end of the code, it auto- matically outdents the text to its original position.
- Choose File➪
You see the Save As dialog box.
- Type py in the File Name field and click Save to save it.
- Choose Run➪Run
A new Python Shell window opens with the text displayed. Even though the text appears on multiple lines in the source code file, it appears on just one line in the output, as shown in Figure 4-19.
Figure 4-18:
The Edit window automati cally indents some types
of text.
Figure 4-19: Use concat enation to make mul tiple lines of text appear on a single line in the output.