Читать книгу Introduction to Python Programming for Business and Social Science Applications - Frederick Kaefer - Страница 98
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’, ‘bc’, ‘c’, 125, True, False, 125, 128]. Line 3: string = ‘This string has 35 characters in it’. Line 5: # The following lines of code print the length of the sequences. Line 6: print(“length of list: ”, len(list)). Line 7: print(“length of string: ”, len(string)). Line 9: # The following lines of code count how many times character “a” is in each. Line 10: print(“number of times ‘a’ in list: ”, list.count(‘a’)). Line 11: print(“number of times ‘a’ in string: ”, string.count(‘a’)). Line 13: # The following lines of code determine if character “e” is in each. Line 14: print(“‘e’ is in list: ”, ‘e’ in list). Line 15: print(“‘e’ is in string: ”, ‘e’ in string). Line 17: # The following lines of code print slice of elements in each. Line 18: print(“slice of 3rd to 7th elements of list: ”, list[2:7]). Line 19: print(“slice of 3rd to 7th elements of string: ”, string[2:7]).
Back to Figure
The output is titled, RESTART: I: Fig 3_11 Commonly used sequence operations.py. There are eight lines of output. Line 1: length of list: 10. Line 2: length of string: 35. Line 3: number of times ‘a’ in list: 1. Line 4: number of times ‘a’ in string: 3. Line 5: ‘e’ is in list: False. Ling 6: ‘e’ is in string: True. Line 7: slice of 3rd to 7th elements of list: [‘b’, ‘bc’, ‘c’, 125, True]. Line 8: slice of 3rd to 7th elements of string: is st.
Back to Figure
There are eight lines of code as follows. Line 1: # This Python code works with a tuple object. Line 3: taxi_ride_info = [“da7a62fce04” , 180, 1.1, True] #This assigns four different objects to the tuple. Line 5: print(“The data type for the taxi_ride_info variable is: ”, type(taxi_ride_info)). Line 7: # The following code prints out the data type of each element of the tuple. Line 8: print(“The data type for the first element of taxi_ride_info is: ”, type(taxi_ride_info[0])). Line 10: print(“The data type for the second element of taxi_ride_info is: ”, type(taxi_ride_info[1])). Line 12: print(“The data type for the third element of taxi_ride_info is: ”, type(taxi_ride_info[2])). Line 14: print(“The data type for the fourth element of taxi_ride_info is: ”, type(taxi_ride_info[3])).
Back to Figure
The output is titled, RESTART: I:\Fig 3_13 AssigningTuples.py. There are five lines of output as follows. Line 1: The data type for the TaxiRideInfo variable is: <class ‘tuple>. 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 five lines of code as follows. Line 1: # This Python code creates a tuple with seven different ages. Line 2: respondent_ages = (55, 28, 24, 34, 59, 22, 19). Line 4: print(“There are “,len(respondent_ages),” respondent ages in the tuple”). Line 5: print(“The oldest respondent was: ”, max(respondent_ages),” years old”). Line 6: print(“The youngest respondent was: ”, min(respondent_ages),” years old”).
Back to Figure
The output is titled, RESTART: I:\Fig 3_15 GSS tuples example.py. There are three lines of output as follows. Line 1: There are 7 respondent ages in the tuple. Line 2: The oldest respondent was : 59 years old. Line 3: The youngest respondent was : 19 years old.
Back to Figure
There are seven lines of code as follows. Line 1: # This Python code creates a dictionary with three entries. Lines 2 through 4: survey_dictionary = {“survey_count”: 1000, “2014_takers”: 43, “2016_takers”: 52}. Line 6: print(“There are “,len(survey_dictionary),” respondents”). Line 7: print(“The largest dictionary key is: ”, max(survey_dictionary)). Line 8: print(“The smallest dictionary key is: ”, min(survey_dictionary)). Lines 10 and 11: print(“The value corresponding to the largest key is: ”, survey_dictionary[max(survey_dictionary)]). Lines 13 and 14: print(“The value corresponding to the smallest key is: ”, survey_dictionary[min(survey_dictionary)]).
Back to Figure
The output is titled, RESTART: I:\Fig 3_17 GSS dictionary example.py. There are five lines of output. Line 1: There are three respondents. Line 2: The largest dictionary key is: survey_count. Line 3: The smallest dictionary key is: 2014_takers. Line 4: The value corresponding to the largest key is: 1000. Line 5: The value corresponding to the smallest key is: 43.
There are three lines of code as follows. Line 1: driver_dictionary = {“Taxi_ID”: 1988, “Driver_Name”: “Mark Ritchie”}. Line 2: # Modify the following line to just print the taxi driver’s name from the dictionary. Line 3: print(driver_dictionary).
Back to Figure
There are 14 lines of code as follows. Line 1: # This Python code creates two dictionaries. Lines 2 and 3: gss_respondents = {“years”: (1972, 1991, 2014), “counts”: (24, 21, 43)}. Line 4: years = dict([1972, 24), (1991, 21), (2014, 43)]). Line 5: print(“line5:”, gss_respondents). Line 6: print(“line6:”, years). Line 8: gss_respondents.update(years). Line 9: print(“line9:”, gss_respondents). Line 11: gss_respondents[2014] = 45. Line 12: print(“line12:”, gss_respondents). Line 13: print(“the value for 1992 is: ”, gss_respondents.get(1992, “no value”)). Line 14: print(“the value for 1991 is: ”, gss_responsents.pop(1991, “no value”)). Line 15: print(“line15:”, gss_respondents). Line 17: gss_respondents.update(years). Line 18: print(“line18:”, gss_respondents).
Back to Figure
The output is titled, RESTART: I:\Fig 3_19 dictionary operations.py. There are eight lines of output as follows. Line 1: line5: {‘years’: (1972, 1991, 2014), ‘counts’: (24, 21, 43)}. Line 2: line6: {1972: 24, 1991: 21, 2014: 43}. Line 3: line9 {‘years’: (1972, 1991, 2014), ‘counts’: (24, 21, 43), 1972: 24, 1991: 21, 2014: 43}. Line 4: line12 {‘years’: (1972, 1991, 2014), ‘counts’: (24, 21, 43), 1972: 24, 1991: 21, 2014: 45}. Line 5: the value for 1992 is: no value. Line 6: the value for 1991 is: 21. Line 7: line15 {‘years’: (1972, 1991, 2014), ‘counts’: (24, 21, 43), 1972: 24, 2014: 45}. Line 8: line18 {‘years’: (1972, 1991, 2014), ‘counts’: (24, 21, 43), 1972: 24, 2014: 43, 1991: 21}.
There are four lines of code as follows. Line 1: gss_respondents = {“counts”: (24, 21, 43)}. Line 2: # Modify the following line to get a list of just the values. Line 3: # in the dictionary using the values() method. Line 4: print(gss_respondents).
Back to Figure
There are 14 lines of code as follows. Line 1: # This Python code uses a function to look up. Line 2: # code meanings for GSS variables using dictionaries. Line 3: def code_lookup(codes): Line 4: region, happy = codes. Line 5: print(“line 5 – region, happy: ”, region, happy). Lines 6 through 9: region_dict = {1: “New England”, 2: “Middle Atlantic”, 3: “East North Central”, 4: “West North Central”, 5: “South Atlantic”, 6: “East South Central”, 7: “West South Central”, 8: “Mountain”, 9: “Pacific”}. Lines 10 and 11: happy_dict = {1: “Very happy”, 2: “Pretty happy”, 3: “Not too happy”, 8: “Don’t know”, 9: “No answer”, 0: “Not applicable”}. Line 12: return(region_dict[region], happy_dict[happy]). Line 14: codes = 3, 2. Line 15: print(“line 15 – codes: ”, codes). Line 16: response = code_lookup(codes). Line 17: print(“line 17 – response: ”, response). Line 18: print(“Interview region: “ + response [0]). Line 19: print(“Happiness level: “ + response[1]).
Back to Figure
The output is titled, RESTART: I:\Fig 3_21 Tuple and dictionary example.py. There are five lines of output as follows. Line 1: line 15 – codes: (3, 2). Line 2: line 5 – region, happy: 3 2. Line 3: line 17 – response: (‘East North Central’, ‘Pretty happy’). Line 4: Interview region: East North Central. Line 5: Happiness level: Pretty happy.
Back to image
There are eight lines of code as follows. Line 1: # This Python code works with a list object. Line 3: taxi_ride_info = [“da7a62fce04” , 180, 1.1, True] #This assigns four different objects to the list. Line 4: print(“The data type for the taxi_ride_info variable is: ”, type(taxi_ride_info)). 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 taxi_ride_info is: ”, type(taxi_ride_info[0])). Line 9: print(“The data type for the second element of taxi_ride_info is: ”, type(taxi_ride_info[1])). Line 11: print(“The data type for the third element of taxi_ride_info is: ”, type(taxi_ride_info[2])). Line 13: print(“The data type for the fourth element of taxi_ride_info is: ”, type(taxi_ride_info[3])).