Introduction to Python Programming for Business and Social Science Applications. Frederick Kaefer
Чтение книги онлайн.
Читать онлайн книгу Introduction to Python Programming for Business and Social Science Applications - Frederick Kaefer страница 25
3.5. Create the tuple of survey respondent IDs from Exercise 3.3 (1012, 1035, 1021, and 1053). Next attempt to add the respondent ID “1011” to the end of the tuple using the .append() method. What happens when you attempt to run the code?
3.6.Write Python code to define a dictionary named taxi_trip_info with the following key value pairs:Print out the dictionary to show what it looks like. Also print out just the Trip_miles value from the dictionary.
3.7. Write Python code to define a list of taxi trip durations in miles (use values 1.1, 0.8, 2.5, 2.6). Also define a tuple of fares for the same number of trips (use values “$6.25,” “$5.25,” “$10.50,” “$8.05”). Store both the tuple and the list as values in a dictionary called trips, with keys “miles” and “fares.” Print out the dictionary to show what it looks like.
References
Python Software Foundation. (2019, June 17). Python 3.7.3 documentation. Retrieved from https://docs.python.org/3/
Zelle, J. M. (2010). Python programming: An introduction to computer science (2nd ed.). Wilsonville, OR: Franklin, Beedle & Associates Incorporated.
Visit study.sagepub.com/researchmethods/statistics/kaefer-intro-to-python for data sets and code to accompany this text!
Descriptions of Images and Figures
Back to Figure
There are eight lines of code as follows. Line 1: # This Python code works with a list object. Line 3: TaxiRideInfo = [“da7a62fce04” , 180, 1.1, True] #This assigns four different objects to the list. Line 4: print(“The data type for the TaxiRideInfo variable is: ”, type(TaxiRideInfo)). Line 6: # The following code prints out the data type of each element of the list. Line 7: print(“The data type for the first element of TaxiRideInfo is: ”, type(TaxiRideInfo[0])). Line 9: print(“The data type for the second element of TaxiRideInfo is: ”, type(TaxiRideInfo[1])). Line 11: print(“The data type for the third element of TaxiRideInfo is: ”, type(TaxiRideInfo[2])). Line 13: print(“The data type for the fourth element of TaxiRideInfo is: ”, type(TaxiRideInfo[3])).
Back to Figure
The output is titled, RESTART: I:\Fig 3_1 AssigningList.py. There are five lines of output as follows. Line 1: The data type for the TaxiRideInfo variable is: <class ‘list’>. Line 2: The data type for the first element of TaxiRideInfo is: <class ‘str’>. Line 3: The data type for the second element of TaxiRideInfo is: <class ‘int’>. Line 4: The data type for the third element of TaxiRideInfo is: <class ‘float’>. Line 5: The data type for the fourth element of TaxiRideInfo is: <class ‘bool’>.
Back to Figure
There are seven lines of codes as follows. Line 1: # The following line of code creates a list object with four elements. Line 2: taxi_ride_info = [“da7a62fce04” , 180, 1.1, True]. Line 3: print(“List values after list creation: ”, taxi_ride_info). Line 5: # The next line of code changes the third element of the list to 1.5. Line 6: taxi_ride_info[2]=1.5. Line 8: # The following line of code displays the values of the list objects. Line 9: print(“List values after changing list element: ”, taxi_ride_info).
Back to Figure
The output is titled, RESTART: I:\Fig 3_3 ChangeListValue.py. There are two lines of output as follows. Line 1: List values after list creation: [‘da7a62ce04’, 180, 1.1, True]. Line 2: List values after changing list element: [‘da7a62fce04’, 180, 1.5, True].
There are three lines of code as follows. Line 1: list_variable = [7, True, 3, “Hello”, 1.5]. Line 2: # Add a value of Python code to change the value True to value False. Line 5: print(list_variable).
Back to Figure
There are 18 lines of code as follows. Line 1: # The next line creates an empty list. Line 2: taxi_ride_info = []. Line 4: # The following lines of code add values to a list. Line 5: taxi_ride_info.append(“da7a62fce04”). Line 6: taxi_ride_info.append(180). Line 7: taxi_ride_info.append(1.1). Line 8: taxi_ride_info.append(True). Line 10: taxi_ride_info.insert(2, True) #This line adds a new value to the list at the third location. Line 12: # The following line of code displays the values of the list objects. Line 13: print(“After appending 4 items and inserting item with true value: ”, taxi_ride_info). Line 15: # The following line removes the first item that has the value True from the list. Line 16: taxi_ride_info.remove(True). Line 18: # The following line of code displays the values of the list objects. Line 19: print(“After removing item with True value: ”, taxi_ride_info). Line 21: # The following line of code removes the last item from the list and assigns it to a variable. Line 22: item_removed = taxi_ride_info.pop(). Line 24: # The following line of code displays the values of the list objects and item removed. Line 25: print(“After using the pop method, List: ”, taxi_ride_info, “Item removed: ”, item_removed).
Back to Figure
The output is titled, RESTART: I:\Fig 3_5 ListMethods.py. There are three lines of output as follows. Line 1: After appending 4 items and inserting item with True value: [‘da7a62fce04’, 180, True, 1.1, True]. After removing item with True value: [‘da7a62fce04’, 180, 1.1, True]. Line 3: After using the pop method, List: [‘da7a62fce04’, 180, 1.1]. Item removed: True.
Back to Figure
There are nine lines of code as follows. Line 1: # First create a Python string variable with “Hey, Taxi!” message. Line 2: str_message = “Hey, Taxi!” Line 4: # The next line demonstrates how slicing of a string object works. Line 5: print (“The second to seventh characters are ”, str_message[1:7]). Line 7: # The following line of code displays the values of the results. Line 8: new_string = “Hey,” + “Taxi!” Line 9: print (“The string resulting from concatenating the two parts is: ”, new_string). Line 11: corrected_string = new_string[0:4] + “ “ + new_string[4:]. Line 12: print (“The corrected string with a blank in the middle is: ”, corrected_string).
Back to Figure
The output is titled, RESTART: I:\Fig 3_7 Python string operations.py. There are three lines of output. Line 1: The second to seventh characters are ey, Ta. Line 2: The string resulting from concatenating the two parts is: Hey,Taxi! Line 3: The corrected string with a blank in the middle is: Hey, Taxi!
Back to Figure
There are 11 lines of code, as follows. Line 1: # First assign trip cost components to string variables. Line 2: fare = “$5.25”. Line 3: tip = “$2.00”. Line 4: extras = “$1.00”. Line 6: # The next use slicing to remove “$” from each string. Line 7: fare = fare[1:]. Line 8: tip = tip[1:]. Line 9: extras = extras[1:]. Line 11: # now add up float values and assign to trip_total variable. Line 12: trip_total = float(fare) + float(tip) + float(extras). Line 13: print (“The total trip cost is: ”, “$” + str(trip_total)).
Back to Figure
There are 15 lines of code as follows. Line 1: # The next two lines create a list and a string. Line 2: list = [‘a’, ‘ab’, ‘b’,