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

Автор: Frederick Kaefer
Издательство: Ingram
Серия:
Жанр произведения: Зарубежная деловая литература
Год издания: 0
isbn: 9781544377452
Скачать книгу
set.

      Chapter Summary

      In this chapter, we introduced the mutable list object and the immutable tuple object. We learned that lists enable us to store multiple values of different data types in the same variable and that each item in a list can be referenced using an index value. We also learned about list methods, which are actions that can be performed to or with lists. Lists are very useful when working with data sets that are made up of different types of data. We also learned about Python string operations, which are very useful when working with portions of strings. We learned that strings are immutable, so we are not able to modify portions of strings in place, but we are able to replace strings by assigning portions of a string or combinations of string portions using concatenation. We then learned how Python sequence operations can be used on strings and lists to do things like searching for values or counting how many times a value appears in a list of values. We also learned about tuples, which are immutable sequences. Tuples have important usages when we have sequences of elements that we do not want to change.

      We also learned about Python dictionaries in this chapter and how we can define one. We learned a number of operations that are used to work with dictionaries to do things like returning information corresponding to a dictionary entry or adding or changing a dictionary entry. We then demonstrated using both tuples and dictionaries alongside each other in our code. We learned that tuples can be very useful for passing multiple values to a function and that dictionaries are very useful for looking up values that correspond to coded data, such as that found in the GSS data set.

      In the next chapter, we will cover control logic and loops, which enable the conditional and repeated execution of code statements but also can make it more challenging to detect and resolve errors. At the end of the next chapter, we will cover error handling as well as techniques that are useful in identifying and resolving logic errors in Python code.

      Glossary

      CharacterThe smallest possible component of a text. Characters vary depending on the language and context used.Compound data typesData types that group together multiple values.ConcatenationOperation that combines multiple strings into one string.DictionaryA data type that contains key-value pairs. Known in other languages as associative arrays or hash tables, this enables the programmer to store multiple related data values in one structure and reference entries by their specified key values.Dictionary keysUsed to locate values within a dictionary, must be an immutable type.Dot operatorUsed to reference a method property of an object.ImmutableHaving a value that is not modifiable.IndexA reference to a specific element of a data structure such as a list.ListAn object that is a collection of elements that are referenceable using an index.MutableHaving a value that is modifiable.SlicingAccesses a portion of a string by referencing a starting index and an ending index value.TupleLike lists, tuples are sequences. However, unlike lists, tuples are immutable, meaning they are not modifiable. This can be useful to ensure integrity of the data throughout the program.

      End-of-Chapter Exercises

       3.1. Identify the names of the functions and methods used in the following code.Description

       3.2. Use the input() function learned in Chapter 2 to input three strings (a first name, a middle initial, and a last name). Concatenate these strings together with a space between each, storing the result in a new variable. Print out the concatenated string.

       3.3. Define a list of survey response values (5, 7, 3, and 8) and store them in a variable. Next define a tuple of respondent ID values (1012, 1035, 1021, and 1053). Use the .append() method to append the tuple to the list. Print out the list. What happened?

       3.4. Create the list of responses from Exercise 3.3 (values 5, 7, 3, 8). Next add the response “0” to the end of the list using the .append() method. Next add the response “6” to the list between the values 7 and 3 (in what will be the third position in the list) using the .insert() method. Print out the list to verify that you made the changes correctly.

       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].