Introduction to Python Programming for Business and Social Science Applications. Frederick Kaefer

Чтение книги онлайн.

Читать онлайн книгу Introduction to Python Programming for Business and Social Science Applications - Frederick Kaefer страница 18

Introduction to Python Programming for Business and Social Science Applications - Frederick Kaefer

Скачать книгу

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

      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

Скачать книгу