Writing Your First Application
In This Chapter
▶ Working with the Integrated DeveLopment Environment (IDLE)
▶ Getting started with IDLE
▶ Writing the first application
▶ Seeing how the first application works
▶ Formatting your application code
▶ Using comments effectively
▶ Working with existing applications
▶ Ending your IDLE session
M |
any people view application development as some sort of magic prac- ticed by wizards called geeks who wave their keyboard to produce
software both great and small. However, the truth is a lot more mundane.
Application development follows a number of processes. It’s more than a strict procedure, but is most definitely not magic of any sort. As Arthur C. Clark once noted, “Any sufficiently advanced technology is indistinguishable from magic.” This chapter is all about removing the magic from the picture and introducing you to the technology. By the time you’re finished with this chapter, you too will be able to develop a simple application (and you won’t use magic to do it).
As with any other task, people use tools to write applications. In the case of Python, you don’t have to use a tool, but using a tool makes the task so much easier that you really will want to use one. In this chapter, you use a tool that comes with Python, the Integrated DeveLopment Environment (IDLE). In the previous chapter, you use the command-line tool to play around with Python a little. However, IDLE goes further than the command line tool and makes it possible to write applications with greater ease.
A vast number of other tools are available for you to use when writing Python applications. This book doesn’t tell you much about them because IDLE per- forms every task needed and it comes with Python. However, as your skills increase, you might find that tools such as Komodo Edit (http://www. activestate.com/komodo-edit/downloads) are easier to work with than IDLE. You can find a great list of these tools at https://wiki.python. org/moin/IntegratedDevelopmentEnvironments.
Understanding the Integrated DeveLopment Environment (IDLE)
You can literally create any Python application you want using just a text editor. As long as the editor outputs pure text rather than formatted text as a word processor does, you can use it to write Python code. However, using a text editor isn’t efficient or straightforward. To make the development pro- cess easier, developers have written Interactive Development Environments (IDEs). The IDE that comes with Python is IDLE. However, many other IDEs are capable of working with Python.
The feature set provided by IDEs varies. In fact, that’s why there are so many of them on the market. IDLE provides a basic feature set that is shared by most IDEs out there. It provides the functionality required to
- Write Python
- Recognize and highlight keywords and certain types of special
- Perform both simple editing (such as cut, copy, and paste) and code- specific editing (such as showing the parentheses that surround an expression).
- Save and open Python
- Browse the Python path to make locating files
- Browse and locate Python
- Perform simple debugging tasks (removing errors from the code).
IDLE differs from the command-line version of Python in that you get a full- fledged GUI and you can accomplish many tasks much more easily through IDLE than through the command line. In addition, the command line doesn’t really offer all the same features as IDLE. Yes, you can debug your application using the command line, but it’s a difficult, error-prone process. Using IDLE is a whole lot easier.
Starting IDLE
You find IDLE in the Python 3.3 folder on your system as IDLE (Python GUI). When you click or double-click this entry (depending on your platform), you see the IDLE editor shown in Figure 4-1. The two lines of text contain informa- tion about the Python host and provide suggestions on the commands you can try. The precise information you see differs by platform. Your screen- shots may differ from mine depending on the version of Python you use, the platform you use, how you have IDLE configured, and how you have your system configured.
Figure 4-1: IDLE pro vides you with a GUI
editing experience
for your application
code.
Using standard commands
IDLE provides all the same commands as the command-line version of Python. It doesn’t list them all because the assumption is that you’ll use the GUI features of IDLE to make things easy. However, if you want, you can type help( ) and press Enter to enter help mode, even though this com- mand isn’t listed as one of the initial commands for IDLE as it is for the command-line version. Figure 4-2 shows the results.
Figure 4-2: You can access all the same commands in IDLE that you can with
the com mand line version.
Understanding color coding
The book doesn’t show the color coding that you see when you type help( ), but you can see it in the editor. Color coding lets you see commands with greater ease and differentiate commands from other sorts of text. Press Enter to get out of help mode. As with the command-line version, you see descrip- tive text each time you perform an action.
Now, type print(‘This is some text.’) and press Enter. You see the expected output, just as you normally would (see Figure 4-3). Notice the color coding, though. The print() command is in purple text to show that it’s a com- mand. The text within the print() command is green to show that it’s data and not a command. The output is shown in blue. The color coding makes things a lot easier, which is just one of many reasons that using IDLE is easier than using the command line.
Figure 4-3: With color coding, you can easily determine the use for each kind of text in an application.