Читать книгу Introduction to Python Programming for Business and Social Science Applications - Frederick Kaefer - Страница 23

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.

Description

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.

Description

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.

Description

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).

Description

Figure 1.9 IDLE Run Menu

Description

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.

Introduction to Python Programming for Business and Social Science Applications

Подняться наверх