It’s time to create your first Python application. Your initial Python Shell window won’t work for creating an application, so you can begin by creating a new Edit window for the application. You’ll type the required commands and then save the file to disk.
The initial Python Shell window is just fine for experimentation, but you need a nice, clean Edit window for typing your first application. The Python Shell window is interactive, which means that it gives you immediate feedback for any commands you type. The Edit window provides a static environment, where you type commands, save them, and then run them after you type enough commands to create an application. The two windows serve dis- tinctly different purposes.
Choose File➪New File to create a new window. A new window like the one shown in Figure 4-11 opens. Notice that the title bar says Python 3.3.4 Untitled instead of Python 3.3.4 Shell. A Python Shell window will always have the word “Shell” in the title bar. The two windows also have some unique toolbar entries. For example, an Edit window includes the Run command, which you use later to test your application.
Use the Edit
window to create applications.
Working with the Edit window is just like working with any other text editor. You have access to basic editing commands such as Copy, Cut, and Paste. Pressing Enter moves to the next line rather than executing a command as it would when working in the Python Shell window. That’s because the Edit window is a static environment — one where you type commands and save them for later reuse.
The Edit window also provides special commands to format the text. The “Understanding the Use of Indentation” and “Adding Comments” sections of this chapter describe how to use the formatting features. What you need to know now is that these formatting commands act differently from those in a standard text editor because they help you control the appearance of code rather than of generic text. Many of the formatting features work automati- cally, so you don’t need to worry about them now.
Finally, the Edit window provides access to commands that tell Python to perform the steps in the procedure you create one at a time. This process is called running the application. The “Running the Application” section of this chapter describes this process in greater detail.
As with the Python Shell window, you can simply type a command into the Edit window. To see how this works, type print(. Notice that the Edit window provides you with helpful information about the print() command, as shown in Figure 4-12. The information is a little terse, so you may not under- stand it now. As the book progresses, you learn more about the print() command and the help provided by the Edit window will make more sense. For now, the word value is the one that you need to focus on. The print() command needs a value before it can print anything and you’ll encounter a host of different values as the book progresses.
The Edit window pro vides helpful information about the commands you type.
Finish the command by typing “This is a simple Python application.”) and pressing Enter. Your application should look like the one shown in Figure 4-13. This is one of the simplest applications you can create using Python.
Figure 4-13: A complete application can be quite
You could run the application now if you wanted to. However, saving your application before you run it is always a good idea. That way, if you make a mistake that causes Python or the system to freeze for some reason, your application code is still safe. Saving the application makes it easier to go back later to determine what went wrong, make corrections, and try running the application again.
Choose File➪Save to display the Save As dialog box, shown in Figure 4-14. The Edit window automatically chooses the Python33 folder to save the application in. However, this is where the Python code resides, and saving your application code in the same folder is a bad idea.
The example code for this book is contained in a folder named BP4D (Beginning Python For Dummies). The code for this chapter is found in the \BP4D\Chapter04 subfolder of the downloadable source (see the
Introduction for the location of the source code online). If you want, create a directory structure with similar names using a technique that works for your platform as you follow along in the book. You can also open the down- loadable source code file for the book and avoid typing the example code.
Type FirstApp.py in the Filename field of the Save As dialog box and click Save. Your application code is now saved on disk and you can access it anytime you want.
Figure 4-14: The Save As dialog box provides the means for saving your application.
When you return to the Edit window, the title bar text changes, as shown in Figure 4-15. Notice that the title bar includes the full path to the application.
Figure 4-15: An applica tion on disk displays its name and path in the title bar.