Читать книгу Introduction to Python Programming for Business and Social Science Applications - Frederick Kaefer - Страница 71

Descriptions of Images and Figures

Оглавление

Back to Figure

There are seven lines of code as follows. Line 1: # This is a comment line that begins with a pound sign “#”. Line 3: # Example 1a: assigning a string value to a variable creates a string object type variable. Line 4: trip_id = “da7a62fce04” # This assignment is assigning a string to a variable named trip_id. Line 5: print(“The data type for the trip_id variable is:”, type(trip_id)). Line 7: #Example 1b: assigning an integer value to a variable creates an integer object type variable. Line 8: trip_seconds = 180. Line 9: print(“The data type for the trip_seconds variable is:”, type(trip_seconds)).

Back to Figure

The output is titled, RESTART: I:/Fig 2_1 Python code example 1.py. There are two lines of output. Line 1: The data type for the trip_id variable is: <class ‘str’>. Line 2: The data type for the trip_seconds variable is: <class ‘int’>.

Back to Figure

There are six lines of codes as follows. Line 1: # Example 2a: assigning a float value to a variable creates a float object type variable. Line 2: trip_miles = 1.1. Line 3: print(“The data type for the trip_miles variable is: ”, type(trip_miles)). Line 5: # Example 2b: assigning a boolean value to a variable creates a boolean object type variable. Line 6: trip_completed = True. Line 7: print(“The data type for the trip_completed variable is: ”, type(trip_completed)).

Back to Figure

The output is titled, RESTART: I:/Fig 2_3 Python code example 2.py. There are two lines of output. Line 1: The data type for the trip_miles variable is: <class ‘float’>. Line 2: The data type for the trip_completed variable is: <class ‘bool’>.

Back to image

There are three lines of code as follows. Line 1: # modify the following line of code so that a string data type variable is created. Line 2: variable_created = 12.5. Line 3: print (“The data type of the variable created is: ”, type(variable_created)).

Back to Figure

There are nine lines of codes, as follows. Line 1: # Create a few variables with numeric values. Line 2: first_number = 4. Line 3: second_number = 8. Line 4: third_number = 2. Line 6: # Two assignment statements with mathematical formulas using the variables. Line 7: result1 = first_number + second_number * third_number. Line 8: result2 = (first_number + second_number) * third_number. Line 10: # The following line of code displays the values of the results. Line 11: print(“Result: ”, result1, “Result2: ”, result2).

Back to image

There are three lines of codes as follows. Line 1: # Add parentheses, a subtraction operator, and an exponentiation operator. Line 2: # so the following code prints the value 25. Line 3: print( 8 3 2 ).

Back to Figure

There are two lines of code as follows. Line 1: value_entered = input(“Please enter a number between 1 and 100: “). Line 2: print(“The value that you entered was: “ value_entered). The statement value_entered in the second line is highlighted. A dialog box, titled SyntaxError, displays the message, invalid syntax. The OK button is at the bottom of the dialog box.

Back to Figure

The output is titled, RESTART: I:\Fig 2_8 Python code with exception.py. There is one line of output as follows. Line 1: please enter a number between 1 and 100: 50. Text under the output continues as follows. Traceback (most recent call last): File “I:\Fig 2_8 Python code with exception.py”, line 4, in <module>. Print(“The value that you entered was: ”, value_entered). NameError: name ‘Print’ is not defined.

Back to Figure

The output is titled, RESTART: I:\Fig 2_10 Python code with TypeError exception.py. Text reads, Traceback (most recent call last): File “I:\Fig 2_10 Python code with TypeError exception.py”, line 3, in <module> result = first_number + second_number.

Back to image

There are five lines of code as follows. Line 1: first_number = 10. Line 2: # Remove the quotation marks in the following line to resolve issue. Line 3: second_number = “20”. Line 4: result = first_number + second_number. Line 5: print (“The result is: ”, result).

Back to Figure

There are seven lines of code as follows. Line 1: # Create a few variables with numeric values. Line 2: first_number = 4. Line 3: second_number = 8. Line 5: # Calculate the average of the numbers by adding them and dividing by 2. Line 6: average = first_number + second_number /2. Line 8: # The following line of code displays the result. Line 9: print(“The average of ”, first_number,” and ”, second_number,” is ”, average).

Back to image

There are five lines of code as follows. Line 1: first_number = 4. Line 2: second_number = 8. Line 4: # Modify the following line by putting parenthesis around the two variables added. Line 5: average = first_number + second_number /2. Line 7: print(“The average of ”, first_number,” and ”, second_number,” is ”, average).

Back to Figure

There are eight lines of codes as follows. Line 1: # First prompt user to enter trip cost components to string variables. Line 2: fare = input(“Please enter the taxi fare: “). Line 3: tip = input (“Please enter the tip amount: “). Lines 5 and 6: print(“The amounts entered for fare and tip was : ”, fare, tip). Lines 7 and 8: print(“The data types for each are: ”, type(fare), type(tip)). Line 10: # now add up float values and assign to trip_total variable. Line 11: trip_total = float(fare)+float(tip). Line 12: print (“The total trip cost is: ”, “$” + str(trip_total)).

Back to Figure

The output is titled, RESTART: I:\Fig 2_14 program to add up trip costs.py. There are five lines of output as follows. Line 1: Please enter the taxi fare: 5.25. Line 2: Please enter the tip amount: 2.00. Line 3: The amounts entered for fare and tip was: 5.25 2.00. Line 4: The data types for each are: <class ‘str’> <class ‘str’>. Line 5: The total trip cost is: $7.25.

Back to Figure

There are three lines in the syntax. Line 1: def function_name, left parenthesis, left square bracket, parameter(s), right square bracket, right parenthesis, colon. Line 2: code_statements. Line 3: left square bracket, return value(s), right square bracket. The terms def and return are in bold, while the other components of the syntax are italicized.

Back to Figure

The first flowchart starts with begin. When the syntax begins, its code statements are processed, leading to function 1 parameters. The parameters are passed to the second flowchart, and Function1 begins. Its code statements are processed, the values are returned to function 1 parameters in the first flowchart, and the process in the second flowchart ends. In the first flowchart, function 1 parameters process the code statements, after which the function ends.

Back to Figure

The output is titled, RESTART: I: \Fig 2_22 error using function with incorrect parameters.py. Text reads, Traceback (most recent call last): File “I:\Fig 2_22 error using function with incorrect parameters.py”, line 4, in <module> hello (“John”, “Joe”). TypeError: hello() takes 1 positional argument but 2 were given.

Back to Figure

There are eight lines of code as follows. Line 1: # The following is the definition of the function Find_Average. Lines 2 and 3: def find_average(first_number, second_number): return (first_number + second_number)/2. Line 5: # The following is executable code. Line 6. # First create a few variables with numeric values. Line 7: number1 = 4. Line 8: number2 = 8. Line 10: # The following line of code displays the values of the results. Line 11: print(“The average of the two numbers is: “,find_average(number1, number2)).

Back to image

There are three lines of code as follows. Line 1: # modify the user-defined function so it prints the average of x and y: Lines 2 and 3: def my_function(x, y): print (x + y). Line 5: my_function(4, 5).

Back to Figure

There are two lines of code as follows. Line 1: # The following is the definition of the function find_average. Lines 2 and 3: def find_average(first_number, second_number): return (first_number + second_number)/2.

Back to Figure

There are seven lines of code as follows. Line 1: import Fig_2_28_Find_Avreage_Module as fam. Line 3: # The following is executable code. Line 4: First create a few variables with numeric values. Line 5: number1 = 4. Line 6: number2 = 8. Line 8: # The following line of code displays the values of the results. Line 9: print(“The average of the two numbers is: ”, fam.find_average(number1, number2)).

Back to image

The first screenshot shows one line of code as follows. Lines 1 and 2: def my_function(x, y): return((int(x) + int(y)) / 2).

The second screenshot shows four lines of code as follows. Line 1: # Add a line to import the UserFunction module with the alias UF. Line 4: value1 = input(“Please enter a value: “). Line 5: value2 = input(“Please enter a second value: “). Line 7: print(“The average of the values entered is: ”, UF.my_function(value1, value2)).

Introduction to Python Programming for Business and Social Science Applications

Подняться наверх