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

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

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

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

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

      The title displayed in the title bar at the top of the shell window is Python 3.7.3 Shell. The options in the ribbon at the top are File, Edit, Shell, Debug, Options, Windows, and Help. The shell window shows three lines of introductory text in the shell window are as follows. Line 1: Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 21:26:53) [MSC. Line 2: Type “help”, “copyright”, “credits” or “license()” for more i. Line 3: >>>. The title displayed in the title bar at the top of the file is Fig 1_6 heytaxi.py - I:\Fig 1_6 heytaxi.py (3.7.3). The options in the ribbon at the top are File, Edit, Format, Run, Options, Windows, and Help. Run option is selected. The expanded menu lists Python Shell, Check Module Alt+X, and Run Module F5. Run Module is selected.

      Back to Figure

      The title displayed in the title bar at the top of the shell window is Python 3.7.3 Shell. The options in the ribbon at the top are File, Edit, Shell, Debug, Options, Window, and Help. There are three lines of introductory text in the shell window, followed by the result of code execution. The result is titled, RESTART: I: \Fig 1_6 heytaxi.py. There is one line of result. Line 1: Hey, Taxi!

      2 Building Blocks of Programming

      Learning Objectives

       Explain good programming practice guidelines

       Describe variables and commonly used data types in Python

       Write code statements that assign values to variables

       Classify types of errors that can occur in Python code

       Develop functions that contain code segments

       Write code statements using Python functions

      Introduction

      Programming in the Python language uses instructions to tell the computer what actions you want performed. This chapter begins by discussing good programming practice. We then explain the basic elements of Python code, beginning with Python keywords, objects, operators and delimiters, data types, and variables. We explain comments and assignment statements next. Python code statements must comply with strict syntax rules in Python and the incorrect specification of code statements result in errors. This chapter explains the three types of errors that often result. Following the discussion of error types, we introduce functions. The packaging of Python code into smaller components helps to organize code and simplify the identification and resolution of errors. We also illustrate commonly used Python built-in functions and their usage. The chapter concludes with the development of a code module used by Python code in a different file.

      Good Programming Practice

      Many references provide advice and recommendations for best practices in programming. We will discuss some basics that can influence your development practices as you learn more about Python. Best practices do not just apply to Python code, and in fact, there are entire books on the subject (i.e., Martin, 2009).

      To begin, consider who else will be reading your code. Is it a coworker or classmate with whom you might work on the same project? Is it someone grading your homework or project? On the other hand, is it your future self, as you may be working on a long-term project? Consider whether your code will be easy to follow and understand to whomever may be reading your code after you write it (Kaefer, 2018).

      To facilitate easy-to-understand code, be sure to use descriptive variable names. While variable names like a, i, or x may be simple to type and illustrate concepts, consider naming your variables after what they represent or their use. For example, customer_name is better than c, and survey_respondant_age is better than x. Be sure to add plenty of helpful comments in your code. Leave comments before functions to explain what they do. Include comments throughout code that may be complex or counterintuitive on a first glance. Above all, be consistent. Avoid using different styles throughout your code and follow any style guidelines that may be in place in your organization.

      Good programming practice is not simply about the readability of your code. A programmer should always consider whether there is a better way to do something. Does your code require doing the same thing repeatedly? A loop is a logical response (we cover loops in Chapter 4). Also, consider breaking the code into functions and having a loop call the function. These are the basics of writing modular code. As the number of lines of code increases, you may want to break it up into modules. The more you interact with code from different sources and read code written by different people, the more clear it will be why following good practices results in better communication among programmers and fewer headaches when trying to debug problems (Martin, 2009; van Rossum, Warsaw, & Coghlan, 2001).

      Lessons learned: In this section, we learned about some practices to follow when developing code, which becomes very important when you work with other people on programming projects.

      Basic Elements of Python Code

      The Python programming language contains basic elements for specifying instructions. When a Python instruction is executed, the interpreter processes the instruction as a command, which is telling the processor what action it needs to perform. Some basic commands include assigning a value to a variable, adding two values together, and, as we saw in our first Python program in Chapter 1, printing a message to the console for a user to read. In order for the interpreter to operate efficiently, a limited number of basic elements are used to specify instructions. This section briefly introduces elements, including Python keywords, objects and classes, operators and delimiters, data types, and variables.

      Python Keywords

      Python keywords are identifiers that are reserved words that the interpreter uses for very specific purposes. As we will see in the chapter, we can create and name variables and functions as part of the instructions that we are writing. However, we are not allowed to use any of the Python reserved words for the names of variables or functions that we are creating. We list some commonly used Python keywords in Table 2.1. We will use all the keywords listed in Table 2.1 within this book. The official list of all Python keywords is in the Python documentation (Python Software Foundation, 2019, “Keywords”).

      Objects and Classes

      Objects are the building blocks of Python, which is an object-oriented programming language. Objects or relations between objects represent all data in a Python program (Python Software Foundation, 2019, “Data Model”). Python classes provide all the standard features of object-oriented programming, and classes provide a means of bundling data and functionality together (Python Software Foundation, 2019, “Classes”). In other words, you use classes to create objects and objects can have functions associated with them. Classes are used to create different types of objects that have specific data types and specific actions associated with them. For example, an object can be numeric and an action that can be performed with that object is to add another number to it. A different class of object would be one that had text-based values. An action that may

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