Figure 1.3 The IDLE Python Shell Window on a Mac
The simplest way to execute Python statements is to type in a Python statement at the shell window command prompt; after the enter key is pressed, the Python interpreter executes the statement. Figure 1.4 illustrates how this works by typing the command print(“Hey, Taxi!”) and pressing enter. Interacting directly with the Python interpreter in this way can be very useful for learning about different Python commands and features. Figure 1.4 also shows four other commands: help, copyright, credits, and license.
Figure 1.4 Executing Python Statement at IDLE Shell Window Command Prompt
Typing help at the command line and pressing enter prompts the user to either enter help() for interactive help or else help(object) for help about a specific object, as shown in Figure 1.5. Figure 1.5 also shows the response for the interactive help, which directs first-time users to a web-based tutorial for the Python version in use, as well as detailed directions on how to use the interactive help utility.
Figure 1.5 Python Help Command at IDLE Shell Window Command Prompt
Lessons learned: In this section, we learned how to write and execute Python code in the IDLE shell window.
Python is case sensitive, which means it interprets uppercase letters (capitals) as different from lowercase letters. As a result, the code statement Print (“Hey, Taxi!”) will result in an error stating that the name “Print” is not defined. This case sensitivity applies to Python reserved words as well as variables and function names, which we will discuss and illustrate in the next chapter. This can sometimes be annoying for beginning Python programmers (but you will quickly adapt), but it greatly reduces the processing of the Python interpreter that would otherwise need to treat all the possible combinations of upper- and lowercase letters the same.
Executing Python Code in Files
Entering in a few lines of Python code interactively is a great way to learn, but programmers typically store their Python code in text files with the file name extension “.py.” We discuss text files in Chapter 5. The convention is to use this file name extension as it identifies the purpose of the file. Figure 1.6 illustrates a text file that we created in a text editor (Notepad++, a free source code editor that can be downloaded from the website https://notepad-plus-plus.org/). We use Notepad++ throughout this book to illustrate Python code with line numbers, so that we can refer to specific lines of code by line number. However, we have written and executed all the code examples in the IDLE IDE.
Figure 1.6 Python Code in a Text File
IDLE has a menu system, which makes interactions more user-friendly. Being able to specify which action to perform by selecting an option from a menu reduces the possibilities of typing errors and is much less technically demanding. The File menu in the Python IDLE Shell window shown in Figure 1.7 has facilities for creating new files, opening files and modules, saving files, and printing.
Figure 1.7 IDLE File Menu
Figure 1.8 illustrates that the Python file from Figure 1.6 is open. The IDLE Python Shell is in the background, and the IDLE text editor is in the foreground with the title bar showing the name and location of the opened file. You can open multiple files in the IDLE editor and use colors to differentiate parts of Python code. In Figure 1.8, the word “print,” which is a Python built-in function, appears in a darker color than the text “Hey, Taxi!” which is a string. In the program, the former appears highlighted in purple; the latter, in green. These colors conform to what is known as the “IDLE classic” color scheme, but the colors used can be customized by selecting from the menu “Options” and then “Configure IDLE,” which displays the Settings dialog box. To customize these colors, in the Settings dialog box, choose the Highlights tab to select different types of objects and change their highlight color. Because of limited color used in the print version of this textbook, colors are not displayed as they actually appear.
Figure 1.8 IDLE Editor Menu with Python File
In addition to being able to edit multiple Python files in different windows, the IDLE menu has an option to run a module (which corresponds to the F5 shortcut key), as depicted in Figure 1.9. A module is a text file that contains Python code. Figure 1.10 illustrates the result of clicking on “Run Module” when the “Fig 1_6 heytaxi.py” file window is active. The output of the execution of the Python code in that file is in the IDLE Python Shell (much as we saw the result appear after the execution of the code instruction in the file from the shell command prompt previously in Figure 1.4).
Figure 1.9 IDLE Run Menu
Figure 1.10 Result of Code Execution in IDLE Python Shell
The example just given, although simplistic in nature, illustrates the ability to execute Python code from plain text files. Using files enables the storing and execution of many lines of code as a program. Other features of the IDLE editor include providing syntax and autocompletion for Python statements as well as debugging features to set breakpoints and step through code. We demonstrate debugging in Appendix B.
SCU 1.1 Introducing Stop, Code, and Understand! Exercises
We incorporate coding exercises throughout the book to make sure you understand the topics that we are covering. Each one uses comments (which begin with the # sign) to indicate precisely where you need to add or change Python code. To complete each exercise, download the corresponding Python file from the companion website, open the file in the Python IDLE editor, make the requested change, and execute the code to verify that you understand the concept. We explain the solutions to the Stop, Code, and Understand! exercises in Appendix D at the end of the book, and the solution files for each exercise are available on the companion website.
For the first exercise, download the file “SCU 1_1.py” from the companion website and save it either on your computer or on a removable storage device. Next, open the Python IDLE shell and then open the file “SCU 1_1.py” in the Python IDLE editor by