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

Автор: Frederick Kaefer
Издательство: Ingram
Серия:
Жанр произведения: Зарубежная деловая литература
Год издания: 0
isbn: 9781544377452
Скачать книгу
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)).

      3 Further Foundations of Python Programming

      Learning Objectives

       Describe compound data types

       Create and use lists and their commonly used methods

       Use string properties and methods

       Create and use tuples to reference multiple items

       Create and use dictionaries of key-value pairs

      Introduction

      This chapter develops a broader foundation of Python programming, covering compound data types, including lists, tuples, and dictionaries, as well as working with string methods and operations. You can use lists, which are indexable collections of objects, to store and access data of various types. We use lists to process data that include strings, numeric values, and Boolean values. We next learn about list methods and how methods are like functions but fundamentally different in their use. We then revisit string objects (introduced in Chapter 2), introducing commonly used string methods that are useful when working with text data. We also go into depth on string operations, which facilitate the manipulation of data that are either entered in by users or read from files and/or the Internet as is covered in later chapters. The next topic covered is tuples, which are like lists but have specific properties and purposes of their own. We then introduce dictionaries, which are key-value mappings that present powerful capabilities for working with data. We conclude the chapter with an example from the General Social Survey (GSS) that uses tuples and dictionaries to report the meaning of coded data. We will see throughout the textbook that these compound data types play a pivotal role in working with data.

      Compound Data Types

      We learned about the Python data types Boolean, integer, float, and string in Chapter 2. Python also has compound data types, which are data types that group multiple values together. This chapter explains and illustrates the use of several compound data types, including lists, tuples, and dictionaries.

      Lists

      A list in Python is an object that is a collection of objects that is referenceable using an index. We create a list object by using an assignment statement in which the right-hand side has multiple values enclosed in square brackets and separated by commas, such as in line 4 of the Python code in Figure 3.1. The list object created on line 4 of Figure 3.1 consists of four objects, the first being a string, the second an integer, the third a float, and the fourth a Boolean-type object. Each of the objects that are in a list can have different data types, which makes working with lists more complex than the previous examples that we have seen that only use more basic Python object data-type variables.

      The data within the list in line 4 represent taxi trip information that we will be working with throughout the textbook. We describe this data set in Chapter 1 (see Tables 1.2 and 1.3). The first item is a unique taxi trip ID (here comprising 11 characters, but the real taxi trip IDs in the data set are 40 characters). The second item is a numeric value representing trip seconds—how long the taxi trip took from start to finish. The third item is a numeric value representing trip miles—the distance traveled from the pickup location to the drop-off location of the taxi trip. The fourth item is a Boolean value that might indicate if a customer uses a credit card to pay a fare.

      Index Values

      When working with lists, we can reference each element using an index value. The index value for the first item in the list is 0, the second item has index 1, and so on. An index is a reference to a specific element of a data structure such as a list. When using a list in Python, the index is specified using square brackets (“[ ]”). For example, say we have a list: passengers = [“John”, “Jane”, “Joe”]. The list has three elements, with index values of 0, 1, and 2. We can access the first element, “John,” through referencing passengers[0]. If we attempt to use an index reference outside of the existing elements in the list, we will get an error. Using passengers[5] will result in a “IndexError: list index out of range.” Likewise, if we try to use an index value like 0.5, we will get a “TypeError: list indices must be integers, not float.”

      We illustrate this mechanism for referencing items in a list in lines 7, 9, 11, and 13 in Figure 3.1.

      A screenshot displays a Python code with a list object.Description

      Figure 3.1 Python Code with List Object

      Figure 3.2 shows the output from executing the Python code in the file Fig 3_1 AssigningList.py (which is in Figure 3.1). Note that the first line of output reports the type of the taxi_ride_info variable to be a list, and each of the following lines of output demonstrates that each item in the taxi_ride_info list has different data types.

      A screenshot displays the output from the execution of a Python code with a list object.Description

      Figure 3.2 Output from Execution of Python Code in Figure 3.1

      As illustrated above, lists in Python can group together multiple values (i.e., are compound data types). Lists are also mutable, meaning that they are an object type for which the values are modifiable. For example, we can change the value of one of the objects in a list while the remaining items in the list stay the same. Figure 3.3 illustrates Python code that changes one item in a list (line 6) and reports the values in the changed list (line 9). Figure 3.4 illustrates the output from the execution of that code example.

      A screenshot displays a Python code to change the value of a list object.Description

      Figure 3.3 Python Code to Change Value of a List Object

      A screenshot displays the output from the execution of a Python code to change the value of a list object.Description

      Figure 3.4 Python Code to Change Value of a List Object

       Stop, Code, and Understand!

      SCU 3.1 Lists

      Download the file “SCU 3_1.py” from the companion website and save it either on your computer or on a removable storage device.