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

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

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

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

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

       Stop, Code, and Understand!

      SCU 3.2 Use a List Method

      Download the file “SCU 3_2.py” from the companion website and save it either on your computer or on a removable storage device. Open the file in the Python IDLE editor and add a line of code to append the value 21 to this list using a list method. Execute the modified program after the change to verify that the revised code runs and produces the correct result.

A screenshot displays three lines of code in Python IDLE editor. Line 1: my_list = [3, 5, 8, 13]. Line 2: # Append a value to the list my_list using a list method: Line 4: print(my_list).

      Lessons learned: In this section, we learned about lists that 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.

       Python Insight

      Python methods and functions are similar yet have an important distinction. As we saw in Chapter 2, functions are subroutines of code that perform a specific task and can return a value. We also can create our own functions to better organize code and enable code reusability. To use a function in Python code, we specify the function name along with any arguments that it might use. Methods also perform a specific task and can return a value, but the important distinction is that methods are directly associated with an object. To use a method, we specify the object and use the dot operator to specify which method to use. We can also create new object types along with methods and properties. Please see The Python Language Reference (Python Software Foundation, 2019, “The Python Language Reference”) for a detailed explanation of the process.

      String Objects

      Even though string objects in Python are more basic data types, they have some similarities to List objects. Strings are sequences of characters, and we can access each character of a string individually, just as we can access each object in a list, using an index. A character is the smallest possible component of a text, and characters vary depending on the language and context used (Python Software Foundation, 2019, “Unicode HOWTO”). For example, if a variable str_message had the value “Hey, Taxi!” it would comprise 10 characters and you could index each character in the string using the index values 0 through 9. In this example, str_message[2] would be the third character, which is “y.”

      String Methods

      Like lists, strings also have many methods. Table 3.2 identifies and gives a description of some commonly used string methods. A complete list of Python string methods is available in the Python documentation (Python Software Foundation, 2019, “String Methods”).

      Unlike lists, strings are immutable, meaning that their values are not able to be modified. The methods listed in Table 3.2 all return values, which in some cases are copies of the string on which we are applying the method. Note that even though we cannot modify a string, we can replace it with another string. Therefore, we can assign the returned copy from the use of a string method back to the string, replacing it.

      String Operations

      In addition to string methods, there are several operations that we can perform on or with strings. One such operation is slicing, which accesses a portion of a string by referencing a starting index and an ending index value. Slicing is important to learn because we are often interested in text-based information that is within larger amounts of text. We also use slicing with lists to reference a specific portion of the list. The syntax of the slicing operation is object[start_index:end_index], and a specific detail to be noted is that the slice returns the elements beginning at the start_index position through the (end_index – 1) position (not the end_index position). Using our str_message variable from above, which has the value “Hey, Taxi!” the slice of that string from the second character to the seventh character is str_message[1:7], which is “ey, Ta.” Another string operation is concatenation, which combines multiple strings into one string. We perform string concatenation using the “+” operator, so we can concatenate the two strings “Hey,” and “Taxi!” and assign the result to a variable using the statement: variable_name = “Hey,” + “Taxi!” We illustrate both examples in Figure 3.7, and the resulting output is in Figure 3.8.

      A screenshot displays nine lines of code in a Python file for Python string operations.Description

      Figure 3.7 Python String Operations

      A screenshot displays the output from the execution of a Python code for string operations.Description

      Figure 3.8 Output from Execution of Python String Operations

      Note that the result of the concatenation in line 8 did not have a space between the “Hey,” and “Taxi!” substrings. One way to insert a blank in the middle of the string is to concatenate the two parts with a space in between. Line 11 in Figure 3.7 accomplishes this by concatenating a blank with the first part and the last part of the string that did not have a blank. The slice operation in the last part of line 11 does not specify an ending index value, which results in the slice going all the way to the end of the string. Because string objects are immutable, there is not an .insert() method (or any other method or operation that would change the string) like there is for list objects.

      Referring to Table 1.2 and Table 1.3 from Chapter 1, there are several fields in the Taxi Trips data set that involve taxi trip cost-related information (fare, tips, extras, and trip_total). These fields have the data type “Money” in the SODA API, but there is not a Python basic data type money. If we assign the value $4.75 to a variable, an error will occur, because the Python interpreter doesn’t recognize that usage of a dollar sign symbol. To address this using built-in functions, we assign the trip-cost-related information to different variables as string values, and then we remove the dollar sign symbol from each string. We then convert the string values to float data types using the built-in function float.

      A screenshot displays a Python code for adding up trip costs.Description

      Figure 3.9 Python Code to Add Up Trip Costs

      Figure 3.9 incorporates several features that we have discussed in this chapter. As mentioned earlier, the trip cost components have a

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