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
Figure 3.10 Output from Execution of Python Code to Add Up Trip Costs
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.
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”).
Table 3.3
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.
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.
Figure 3.12 Output from Executing Sequence Operations
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.
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),