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

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

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

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

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

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 statements specify what value something is to take. You read assignment statements in Python code from right to left, storing the value on the right-hand side of the equal sign in the variable on the left-hand side of the equal sign, following the syntax:

       variable_name = value

      For example, taxi_number = 333 for an integer variable or my_name = “John Doe” for a string variable. If you want one variable to contain the same values as another variable, you can do so like in the following code statement: other_survey_values = survey_values.

      Code Examples

      We now present a couple of code examples to illustrate the use of the elements just discussed. Figure 2.1 illustrates some Python code statements in a file (Fig 2_1 Python code example 1.py).

      A screenshot of an example of a Python code in a Python file displays seven lines of code.Description

      Figure 2.1 Python Code Example 1

      Several details in the Python code in Figure 2.1 are fundamental to writing programs in Python. Lines 1, 3, and 5 have comments that take up an entire line, and line 4 shows an example of a comment that makes a note at the end of a line of code. Line 4 is an assignment statement in which the right-hand side is a value in quotes. The value within quotation marks is a string-type value. When the code in line 4 executes, the trip_id variable will be a string. Line 5 illustrates the use of two built-in functions to report the data type of the just created trip_id variable. Specifically, a type function is within a print function. We already used the Python built-in print function in the example presented in Chapter 1. The type function in Python determines the data type of an object. Line 8 of the Python code in Figure 2.1 is an assignment statement in which the right-hand side is a number. When the code in line 8 executes, the trip_seconds variable will be an integer. The type function is also in line 9, which reports the data type for the just constructed trip_seconds variable. The output of these code statements is in Figure 2.2.

      A screenshot displays the output from the execution of a Python code.Description

      Figure 2.2 Output from Execution of Python Code in Figure 2.1

      Examining the output shown in Figure 2.2, we see that the data type for the trip_id variable is a string and the data type for the trip_seconds

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