Beginning Programming with Python For Dummies
Book image
Explore Book Buy On Amazon
It’s time to create your first Python application. Your initial IDLE 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.

Open a new window

The initial IDLE Shell window is just fine for experimentation, but you need a nice, clean Edit window for typing your first application. The IDLE 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 distinctly different purposes.

Choose File→New File to create a new window. A new window opens. Notice that the title bar says untitled instead of IDLE Shell 3.1.0. 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.

image0.jpg

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. 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 automatically, 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.

Typing the command

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. The information is a little terse, so you may not understand it now.

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.

image1.jpg

Finish the command by typing “This is a simple Python application.”) and pressing Enter. This is one of the simplest applications you can create using Python.

image2.jpg

Saving the file

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. The Edit window automatically chooses the Python3 10 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.

If you want, create a directory structure with similar names using a technique that works for your platform as you follow along.

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.

image3.jpg

When you return to the Edit window, the title bar text changes. Notice that the title bar includes the full path to the application.

image4.jpg

About This Article

This article can be found in the category: