• No results found

The IDLE Development Environment Shell

IDLE stands for Interactive Development Environment. This development environment provides a built-in text editor, an interactive shell, and many features that assist in the creation and testing of Python scripts.

To start IDLE in the GUI, you navigate to the Python 3 icon located in the Raspberry menu’s (the button with the raspberry on it) Programming menu. Figure 3.5 shows the IDLE Python interactive mode (or Shell) for Python v3.

Figure 3.5 Python v3 IDLE in interactive mode (Shell).

By the Way: Add IDLE to the Desktop

Because you will be using IDLE a great deal in this tutorial, you should add a shortcut icon to your desktop. Navigate to the Python 3 icon located in the

Raspberry menu’s Programming menu, and right-click the Python 3 icon. Select Add to Desktop from the drop-down menu.

The IDLE window’s title bar says Python Shell. Notice that this window uses exactly the same verbiage as the Python interactive shell. This is because the IDLE environment uses the Python interactive shell for this development mode, which is called interactive mode or the Shell.

Did You Know?: IDLE Everywhere

One of the great things about learning IDLE is that this development environment is not just available on Linux. It also is available on Windows and OS X.

Interactive mode has many features that help in the creation and testing of Python scripts.

There are lots of features in IDLE. The following are a few of the most important ones to help you get started in Python programming:

Menu-driven options and their matching control keys—For example, to exit

IDLE, you can click the File menu option and then select Exit from the drop-down menu. To use the control keys to exit IDLE instead of using the menu, you can press the Ctrl+Q key sequence.

Basic text editor—To type a Python script, you can open a new window from the main interactive IDLE Shell window (by pressing the Ctrl+N key sequence) to get access to a basic text editor. The text editor enables you to take such actions as cut and paste text using menu-driven selections or control keys.

Code completion—As you type Python statements, helpful hints appear on the screen, making recommendations on how to finish the syntax you’ve started.

Syntax checking—When you enter a command and press Enter, the Python interpreter checks the syntax of your statement and reports any problems

immediately. This is much better than finding out about syntax errors after an entire script is written.

Color coding—The IDLE environment color-codes syntax as you type it to help you follow the logic of your Python statements. Table 3.1 shows the color codes it uses.

Table 3.1 IDLE Color Codes

Indentation support—Python requires the use of indentation for some of its

constructs. The IDLE shell recognizes these required indentations and automatically provides them for you. (For more information on indentation, see Hour 6,

“Controlling Your Program.”)

Debugger features—The term debugging refers to removing incorrect syntax or logic from a script. With IDLE, the Python interpreter’s syntax checking typically finds syntax errors. You can uncover logic problems by using the IDLE Debugger, which allows you to step through a script instead of adding more Python statements to debug.

Help—Because everyone needs a little help, IDLE provides a nice help facility. You can access the help facility by selecting the Help menu option on the menu bar of the IDLE window and clicking IDLE Help in the drop-down menu.

Of course, trying IDLE’s features yourself will help you better learn to use the IDLE tool.

The following “Try It Yourself” gives you an opportunity.

Try It Yourself: Explore the Python IDLE Tool

In the following steps, you’ll try a couple of the IDLE tool’s features. Don’t be overwhelmed by all the bells and whistles of this tool. Follow these steps to test the basic features and take a look around the environment:

1. If you have not already done so, power up your Raspberry Pi and log in to the system.

2. If you do not have the GUI started automatically at boot, start it now by typing startx and pressing Enter.

3. Open IDLE by double-clicking the Python 3 icon shortcut you added to your desktop (if you did add it to your desktop) or by clicking the Raspberry menu icon, hovering over the Programming menu option, and clicking the Python 3 menu option. You are now in the main IDLE interactive mode window.

By the Way: Python 3 Not Python 2

You might notice a Python 2 icon alongside the Python 3 menu option. This

selection offers the IDLE shell for Python v2. Be sure to select Python 3 to stay on course with this hour.

4. In the IDLE window, at the >>> prompt, type print and then pause and look at the screen. You should notice that the print command has been colored violet.

This is because the print statement is considered a built-in function in Python.

(You will be learning more about the various built-in functions in the coming hours.) The color is provided to help you recognize the syntax of your Python statement and assist in the logic of your scripts. Look back to Table 3.1 for a reminder of the various IDLE color codes.

5. Press the space bar and type (“This is my first Python and then pause again and look at the screen. You should notice that the text "This is my first Python is colored green because Python considers it a string literal. (You will learn more about string literals, too, in the coming hours. For now, just notice the color.)

6. Instead of correctly finishing your Python statement, just press the Enter key.

(You are deliberately trying to generate a syntax error to see how IDLE handles syntactical problems.) You should get the message SyntaxError: EOL while scanning string literal. This is because you did not correctly close the print function. (Well, actually, you were just following directions.)

7. In the IDLE window, type print( and then pause. You should see a screen tip appear in your window, similar to the one shown in Figure 3.6. IDLE attempts to help you by giving guidance via screen tips.

Figure 3.6 An IDLE script tip.

8. Finish the Python statement by typing “This is my first Python statement in IDLE”). Look at your Python statement and make sure it reads print("This is my first Python statement in IDLE").

If you do not have it correct, then modify it by using the left- and right-arrow keys and the Delete key. When you are sure it is correct, press Enter. You should see output similar to what is displayed in Figure 3.7. Congratulations! You just correctly entered your first Python statement in IDLE.

Figure 3.7 Output from a Python statement in IDLE.

9. Finally, exit the IDLE shell by pressing the key combination Ctrl+Q. The IDLE interactive mode window should close.

Did You Know?: Exiting IDLE

You can leave the IDLE Shell a couple different ways. As you did in step 9, you can use the key combination Ctrl+Q to exit. Also, you can use the menu options in IDLE to leave: To do so, click the File menu and then select Exit. The third way is to enter the Python statement exit(). When you do this, you get a pop-up

window titled Kill? that says The program is still running! Do you want to kill it?; then you can press the OK button. This last option is a little violent, but it gets you out of IDLE interactive mode and back to the GUI.

Now that you’ve played with IDLE a bit, its basic features should be more useful to you. As your experience with Python grows, you might want to try some of the IDLE power-user features as well.

By the Way: More IDLE, Please

The official Python website maintains an IDLE document that is worth exploring for more info on using IDLE. You can find it at docs.python.org/3/library/idle.html.