Introduction to Python Programming for Business and Social Science Applications. Frederick Kaefer

Чтение книги онлайн.

Читать онлайн книгу Introduction to Python Programming for Business and Social Science Applications - Frederick Kaefer страница 5

Introduction to Python Programming for Business and Social Science Applications - Frederick Kaefer

Скачать книгу

      Python Versions

      Python versions are numbered A.B.C., where A is the major version, B is a minor version number (for incremental changes), and C is a micro-level number (for bug fixes; Python Software Foundation, 2019, “General Python FAQ”). Release Version 3.x has significant changes from release Version 2.x, and code written in each version is not compatible with the other version. Figure 1.1 shows that the release date for release Version 2.7.17 is October 19, 2019. There are still new updates for Python Version 2.x for those who have developed Python code in the past and do not want to make the changes necessary for that code to be compatible with Version 3.x. Python Version 3.x is the recommended version as Version 2.x, although still widely used, will no longer be maintained after January 1, 2020 (Python Software Foundation, 2019, “General Python FAQ”). This book uses Python Version 3.7, and we have tested all Python code in the book using that specific version. If you use either an older or a newer version of Python, there may be some issues with code execution.

      Lessons learned: In this section, we learned how to install Python on our computer and that there are different versions of Python. We also learned that using different operating systems and different versions of Python can affect how we write and execute Python code.

      Executing Python Code in the IDLE Shell Window

      After downloading and installing the Python development environment, you can verify that it is properly functioning. The standard distribution of Python comes with an Interactive Development Environment (IDE) named IDLE (for Integrated Development and Learning Environment). An Interactive Development Environment (IDE) contains facilities for writing and editing code as well as testing and debugging code. IDLE runs on Windows, Mac OS X, and UNIX. Documentation for IDLE can be found at https://docs.python.org/3/library/idle.html. The IDLE Python shell window is an interactive interpreter that can execute lines of Python code one at a time. Figure 1.2 is the IDLE Python shell console on Windows, and Figure 1.3 is the IDLE Python shell console on a Mac. Although mostly similar, there are platform-specific variations. For consistency, we will be using Windows-based Python illustrations throughout the remainder of this textbook.

      A screenshot displays IDLE Python shell window on Windows with three lines of introductory text.Description

      Figure 1.2 The IDLE Python Shell Window on Windows

      A screenshot displays IDLE Python shell window on Mac with four lines of introductory text.Description

      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.

      A screenshot displays the execution of a Python statement in IDLE Python shell window command prompt.Description

      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.

      A screenshot displays IDLE shell window command prompt displaying the Python help command.Description

      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 Insight

      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.

      A screenshot displays a Notepad file with a Python code.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.

      A screenshot displays the options in the expanded menu of the File option in a Python shell window.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

Скачать книгу