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

Автор: Frederick Kaefer
Издательство: Ingram
Серия:
Жанр произведения: Зарубежная деловая литература
Год издания: 0
isbn: 9781544377452
Скачать книгу
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 variable is an integer. The output messages specifically indicate that trip_id is of class “str” and that trip_seconds is of class “int.”

      Our next example is like the first example but illustrates the construction of float and Boolean variables. Line 2 in Figure 2.3 assigns the value 1.1 to the variable trip_miles and line 3 prints out the data type of the trip_miles variable. Line 6 assigns the value True to the variable trip_completed. Note that True is a Python keyword shown in Table 2.1 and is in blue font in Figure 2.3. Also note that there are not parentheses around True, because if there were, it would be a string instead of the logical value True. Line 7 then prints out the data type of the trip_completed variable.

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

      Figure 2.3 Python Code Example 2

      Examining the output shown in Figure 2.4, we see that the data type for the trip_miles variable is in fact a float and the data type for the trip_miles variable is a Boolean.

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

      Figure 2.4 Output from Executing Code Example 2

       Stop, Code, and Understand!

      SCU 2.1 Variable Assignment

      Download the file “SCU 2_1.py” from the companion website and save it either on your computer or on a removable storage device. Open the file in the Python IDLE editor and execute the program to see that it creates a variable that is a float data type. Add quotation marks around the value 12.5 in the assignment statement in the line indicated so that the type of variable created is a string data type. Execute the modified program to verify that the revised code creates a variable with a string data type.

      Mathematical Expressions

      In the previous section, we saw several examples where assignment statements created variables of different data types based on the values assigned to them. In addition to literal values, we use mathematical expressions to assign values to variables. When writing mathematical expressions, we use arithmetic operators, shown in Table 2.4. Arithmetic operators include symbols to add, subtract, multiply, divide, and exponentiate (raise to a power) values.

      When writing mathematical expressions, you must be careful with the order of operations, which follow the PEMDAS rule (Parenthesis first, then Exponentiation, next Multiplication and Division, and then Addition and Subtraction). Figure 2.5 illustrates how adding two numbers and multiplying by a third number can lead to two different results.

      Figure 2.6 shows the output that results from the execution of the Python code in Figure 2.5. The first result (result1) of the value 20 reflects the fact that the multiplication of the second and third numbers occurs first before the addition of the first number to the result of the multiplication. The second result (result2) of the value 24 reflects the fact that the addition of the first two numbers occurs before the multiplication of the intermediate result and the third number. This example illustrates why we must be careful when applying mathematical expressions. When in doubt, we can use extra parentheses to make sure that specific calculations occur before others within an expression.

      A screenshot displays a Python file illustrating the PEMDAS example.Description

      Figure 2.5 PEMDAS Example

A screenshot displays the output from the execution of the Python code illustrating the PEMDAS example. The output is titled RESTART: I:\Fig 2_5 PEMDAS example.py. There is one line of output as follows. Line 1: Result1: 20 Result2: 24.

      Figure 2.6 Output from Execution of PEMDAS Example

       Stop, Code, and Understand!

      SCU 2.2 Mathematical Expressions

      Download the file “SCU 2_2.py” from the companion website and save it either on your computer or on a removable storage device. Open the file in the Python IDLE editor and add parentheses around two values, a subtraction operator, and an exponentiation operator to the code below so that the code prints the value 25. Execute the modified program after the change to verify that the revised code runs and produces the correct result.