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

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

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

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

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

basic numeric Python data-type objects. Lines 3, 4, and 5 assign the values as strings to variables. Next, we use the slicing operation in lines 8, 9, and 10 to remove the first character of each of the strings. Note that because strings are immutable, we cannot change them directly, but in these operations, we are assigning the modified (sliced) string back to the original variable, replacing it. Line 13 uses the float built-in Python function to convert each of the trip cost components to float values and adds these converted values together and assigns the result of the addition to the trip_total variable. The trip_total variable is a float data-type object, and we convert it to a string in line 14 (using the Python built-in function str) to concatenate it with a dollar sign to report back to the user. Figure 3.10 illustrates the output from executing this Python code.

A screenshot displays the output from the execution of a Python code for adding up trip costs. The output is titled, RESTART: I:\Fig 3_9 program to add up trip costs.py. There is one line of output as follows. Line 1: The total trip cost is $8.25.

      Figure 3.10 Output from Execution of Python Code to Add Up Trip Costs

       Stop, Code, and Understand!

      SCU 3.3 String Operations

      Download the file “SCU 3_3.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 that uses the .upper() method to convert the string my_string to uppercase. 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 as follows. Line 1: my_string = “hello”. Line 2: # Use the .upper() method to conver the string my_string to uppercase: Line 4: print(my_string).

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

      Sequence Operations

      In the previous section, we saw the usefulness of the string slicing and concatenation operations. These operations are also useful with lists, because lists are sequences of elements. We list other commonly used sequence operations in Table 3.3. Note that the first three (in, not in, and +) use operators, the next two use indexing and slicing as described earlier, the next three (len, min, and max) use functions, and the last operation employs a method to determine how many times an object occurs within the specified sequence. Additional sequence operations as well as additional details pertaining to the operations in the table are in the official Python documentation (Python Software Foundation, 2019, “Common Sequence Operations”).

      The Python code in Figure 3.11 illustrates the use of several of these sequence operations on both a string and a list. Lines 2 and 3 of the code in Figure 3.11 construct the list and string, respectively. Lines 6 and 7 use the len function to determine how many elements are in the list and string sequences. Lines 10 and 11 use the .count() method of each sequence type to determine how many times the character “a” appears in each sequence. Lines 14 and 15 determine if the character “e” is in each sequence. Lines 18 and 19 use the slicing operation to report the third to seventh elements in each sequence.

      A screenshot displays a Python file with code lines for commonly used sequence operations.Description

      Figure 3.11 Commonly Used Sequence Operations

      Figure 3.12 illustrates the output of executing the Python code. Interestingly, the length of the list is only 10, whereas the length of the string is 35, even though the specification of the list appears longer in the code. The explanation for this is that the len function is counting the number of elements, and in the list, some of the elements are several characters or numeric digits. The string length is determined by the number of characters in the string, which includes the spaces between the letters in addition to the string’s alphanumeric characters. Perhaps more surprising is the next output that reports the character “a” appears only once in the list but three times in the string. Looking at the list, we see the letter “a” twice, right at the beginning. However, the second instance is the string “ab” and not solely the character “a,” so it is not a “match” for what we are counting. Because the string is a sequence of characters, the interpreter examines each character individually, and thus the result is 3, as the character “a” appears three times in the string. The next output reports that “e” does not appear in the list but does appear in the string. When looking at line 2 and the construction of the list, we can see the letter “e” in the True and False elements. However, these are Boolean values and so are not a match when checking to see if the character “e” is in the list. The string operates differently, due to the fact that the interpreter examines each character individually. Finally, the output for the slicing operation (recall that the index 2 actually references the third element in the sequence, and even though the index 7 references the eighth element in each sequence, the last value specified in the slice is not included) shows that for a list, five list elements are returned, some being strings, a number, and a Boolean value, whereas for a string, five characters are returned (including a space in the middle in this example). These and other sequence operations are extremely useful in Python, and we will see more applications later in this chapter as well as in later chapters of the textbook.

      A screenshot displays the output from the execution of sequence operations.Description

      Figure 3.12 Output from Executing Sequence Operations

       Stop, Code, and Understand!

      SCU 3.4 Sequence Operations

      Download the file “SCU 3_4.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 Python code where indicated to print out the largest value in the list stored in the variable list_variable. Execute the modified program after the change to verify that the revised code runs and produces the correct result.

A screenshot displays two lines of code in Python IDLE editor as follows. Line 1: list_variable = [12, 135, 15, 1456, 7]. Line 2: # Add a line of Python code to print out the largest value in the list.

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

      Tuples

      We commonly use tuples parameters to functions and for values that functions return. A tuple in Python is like a list (an object that is a collection of objects referenceable using an index), but unlike a list (which is mutable),

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