Introduction to Python Programming for Business and Social Science Applications. Frederick Kaefer. Читать онлайн. Newlib. NEWLIB.NET

Автор: Frederick Kaefer
Издательство: Ingram
Серия:
Жанр произведения: Зарубежная деловая литература
Год издания: 0
isbn: 9781544377452
Скачать книгу
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 be associated with that type of object could be to make it all uppercase (made up of all capital letters). This action would not apply to a numeric valued object. A method is an action that you can perform to or with an object. We will explain methods in more detail in the next chapter and see examples of different methods that are used with different types of objects. To learn more about the inner workings of Python, please see The Python Language Reference found at https://docs.python.org/3/reference/index.html.

      Operators and Delimiters

      Operators and delimiters are special symbols that the Python interpreter uses to perform operations and to separate items. Both operators and delimiters work with other elements. Table 2.2 shows commonly used operators and delimiters. We will use these delimiters and operators within this book. For example, the “+” operator is used when we want to add two values together and the “,” delimiter is used to separate two items. The interpreter needs to have something (either an operator or a delimiter) between two distinct items, or it will not understand what it is being instructed to do with those items. The Python Language Reference specifies additional Python operators and delimiters (https://docs.python.org/3/reference/index.html).

      Data Types

      A data type determines what kind of values a piece of data can have and what kind of operations you can perform on the data. Table 2.3 presents the commonly used data types in Python.

      Boolean data-type variables can store either True or False logical values. We will use variables of this data type in Chapter 4 when evaluating conditions that involve comparisons. Programmers use Integer data-type variables to store whole numbers. Variables of this data type are very useful for counting. Float data-type variables are also known as floating-point variables and can store decimal values. This type of variable is useful for situations where there can be fractional values, which often result when dividing numbers. Programmers use string data-type variables to store text that consists of letters, special symbols, and numbers. You enclose strings in quotes to reference them in code. Strings are very useful for composing messages to communicate to users and for creating labels used when formatting output results.

      Variables

      We use variables to store and access values that we are working with. The values of variables can change, or vary, as a program executes. There are some restrictions with naming of variables, including (1) they cannot have the same name as a Python keyword, (2) they cannot have any spaces or operators or delimiters within them, and (3) they cannot begin with a number.

      You do not formally define variables in Python (which most programming languages require). In Python, you create a variable when you first use it. The variable name that you use when something is created refers to an object that is constructed for the purpose that you are specifying. The value that you store in a variable determines the variable’s data type and the actions that can be performed to or with the corresponding object that it refers to.

      Lessons learned: In this section, we learned about basic elements of Python code, including Python keywords, objects and classes, variables and data types, and operators and delimiters. From this point onward, we will combine these basic elements into various statements that make up our code.

      Python Code Statements

      Now that we are familiar with basic elements of Python code, we can begin to write instructions using these elements. However, there are several additional details to discuss first, including comments and variable assignment.

      Comments

      Comments are very important for documenting Python code. Comments in Python code begin with a pound sign (#), and the interpreter does not process them. Comments explain how the code works for those reading the code and can be an entire line or just a note at the end of a line of code. Multiple line comments (also known as documentation strings) can be written beginning with three quotes (“““) and ending with three quotes.

      Variable Assignment

      Assignment