Introduction to Python Programming for Business and Social Science Applications. Frederick Kaefer
Чтение книги онлайн.
Читать онлайн книгу Introduction to Python Programming for Business and Social Science Applications - Frederick Kaefer страница 11
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.
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.
Figure 2.4 Output from Executing Code Example 2
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.
Table 2.4
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.
Figure 2.5 PEMDAS Example
Figure 2.6 Output from Execution of PEMDAS Example
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.
Lessons learned: In this section, we learned about how to use variables in assignment statements, using print statements to visualize our results. We also learned how to use the Python type function to determine and report the data type of an object in a Python program and why the order of operations is important when we have Python code that performs calculations.
Errors
Writing Python code often results in three types of errors: syntax errors, exceptions, and logic errors. We explain and illustrate each of these error types in the following sections.
Syntax Errors
Syntax errors occur when the Python code does not follow the rules that dictate how to write Python code statements. The interpreter identifies syntax errors and highlights the cause of the syntax error in red font in the Python shell window, as shown in Figure 2.7. When a syntax error exists in Python code, the code will not execute until you resolve the syntax error. The cause of the syntax error in this example is that there is nothing combining the string and the variable symbol within the print statement (such as a comma “,”).
Figure 2.7 Python Syntax Error
SCU 2.3 Syntax Errors
Download the file “SCU 2_3.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