Introduction to Python Programming for Business and Social Science Applications. Frederick Kaefer
Чтение книги онлайн.
Читать онлайн книгу Introduction to Python Programming for Business and Social Science Applications - Frederick Kaefer страница 6
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 clicking on File/Open from the menu and selecting the file you just saved. Run the code by clicking on Run/Run Module from the menu. Next, add a line of Python code where indicated in the following program to print out the message “I would like to go to the airport.” Run the revised code by clicking on Run/Run Module from the menu to verify that your added code works correctly.
Lessons learned: In this section, we learned how to write and execute Python code that we save in plain text files. Using files enables the storing and execution of many lines of code as a program that we can save and run later.
Package Managers
A package manager is a program to install libraries of code. These libraries, or packages, contain previously developed code. Once installed, the code found in the package is available to other Python code, saving a great deal of time and effort. Using a package not only prevents “reinventing the wheel” but also usually benefits from the prior development and testing by an entire community of developers. Python comes with a package manager named pip already installed (in Versions 3.4 and later). The Python Software Foundation, a nonprofit company, maintains documentation for pip, which is online at the website https://pip.pypa.io/en/stable/. We will be using pip to install several packages throughout this textbook.
Another way to set up a Python development environment is to install a Python distribution, such as the Anaconda distribution, found at the following URL: https://www.anaconda.com/download/. Python distributions are alternative bundles and are modified packages that include additional functionality. Alternative bundles may not include the latest versions of Python or other libraries and are not maintained by the core Python team (Python Software Foundation, 2019, “Alternative bundles”). We use the pip package manager to install individual packages in this textbook. Learning to install individual packages is an important skill for people programming in Python, which enables the use of packages developed for use within organizations and for packages that are not in any Python distributions.
Lessons learned: In this section, we learned how to use package managers for convenient organization and management of libraries of code. Learning to install individual packages is an important skill to take advantage of the Python packages for both business and social sciences purposes that are available to the Python programming community.
Data Sets Used Throughout the Book
We use two data sets throughout this book to illustrate numerous issues faced when working with data. The data sets are the City of Chicago’s Taxi Trips data set and data from the General Social Survey. We begin with some simple examples in the next chapter to become acquainted with the nature of the data in these data sets, and in later chapters, we work directly with files containing the data sets as well as retrieve the data directly from the World Wide Web. These data sets provide the basis for our coverage of topics later in the book, including statistical analysis, data visualization, and machine learning.
Taxi Trips Data Set
The Chicago Taxi Trips data set has over 100 million records with 26 fields (variables) per record. Table 1.2 presents a subset of fields and their meaning as described in the Taxi Trips documentation (Levy, 2017). We will later see that the formatting of the data in the taxi trip data set is going to present some challenges when working with the data in Python. On a positive note, these challenges working with real data provide a means of learning practical insights into programming with Python. Table 1.3 has sample data selected from the taxi trips data set that correspond to the fields in Table 1.2.
Table 1.2
Table 1.3
General Social Survey (GSS) Data Set
The General Social Survey has over 5,000 variables collected over a period of more than 40 years. You can explore the