Module 2 Python 25 Scheme Syallabus Notes
Module 2 Python 25 Scheme Syallabus Notes
Data Types
5.1 Strings
So far we have seen built-in types like int, float, bool, str and we’ve seen lists and pairs. Strings, lists, and
pairs are qualitatively different from the others because they are made up of smaller pieces. In the case of strings,
they’re made up of smaller strings each containing one character.
Types that comprise smaller pieces are called compound data types. Depending on what we are doing, we may want
to treat a compound data type as a single thing, or we may want to access its parts. This ambiguity is useful.
We previously saw that each turtle instance has its own attributes and a number of methods that can be applied to the
instance. For example, we could set the turtle’s color, and we wrote tess.turn(90).
Just like a turtle, a string is also an object. So each string instance has its own attributes and methods.
For example:
upper is a method that can be invoked on any string object to create a new string, in which all the characters are in
uppercase. (The original string our_string remains unchanged.)
There are also methods such as lower, capitalize, and swapcase that do other interesting stuff.
To learn what methods are available, you can consult the Help documentation, look for string methods, and read the
documentation. Or, if you’re a bit lazier, simply type the following into an editor like Spyder or PyScripter script:
91
How to Think Like a Computer Scientist: Learning with Python 3 Documentation, Release 3rd
Edition
When you type the period to select one of the methods of our_string, your editor might pop up a selection window
— typically by pressing Tab — showing all the methods (there are around 70 of them — thank goodness we’ll only
use a few of those!) that could be used on your string.
When you type the name of the method, some further help about its parameter and return type, and its docstring,
may be displayed by your scripting environments (for instance, in a Jupyter notebook you can get this inofrmation by
pressing Shift+Tab after a function name).
The indexing operator (Python uses square brackets to enclose the index) selects a single character substring from a
string:
The expression fruit[1] selects character number 1 from fruit, and creates a new string containing just this one
character. The variable letter refers to the result. When we display letter, we could get a surprise:
Computer scientists always start counting from zero! The letter at subscript position zero of "banana" is b. So at
position [1] we have the letter a.
If we want to access the zero-eth letter of a string, we just place 0, or any expression that evaluates to 0, inbetween the
brackets:
The expression in brackets is called an index. An index specifies a member of an ordered collection, in this case the
collection of characters in the string. The index indicates which one you want, hence the name. It can be any integer
expression.
We can use enumerate to visualize the indices:
Do not worry about enumerate at this point, we will see more of it in the chapter on lists.
Note that indexing returns a string — Python has no special type for a single character. It is just a string of length 1.
We’ve also seen lists previously. The same indexing notation works to extract elements from a list:
>>> prime_numbers = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31]
>>> prime_numbers[4]
11
>>> friends = ["Joe", "Zoe", "Brad", "Angelina", "Zuki", "Thandi", "Paris"]
>>> friends[3]
'Angelina'
5.1.4 Length
The len function, when applied to a string, returns the number of characters in a string:
To get the last letter of a string, you might be tempted to try something like this:
5.1. Strings 93
How to Think Like a Computer Scientist: Learning with Python 3 Documentation, Release 3rd
Edition
1 size = len(word)
2 last = word[size] # ERROR!
That won’t work. It causes the runtime error IndexError: string index out of range. The reason is
that there is no character at index position 6 in "banana". Because we start counting at zero, the six indexes are
numbered 0 to 5. To get the last character, we have to subtract 1 from the length of word:
1 size = len(word)
2 last = word[size-1]
Alternatively, we can use negative indices, which count backward from the end of the string. The expression
word[-1] yields the last letter, word[-2] yields the second to last, and so on.
As you might have guessed, indexing with a negative index also works like this for lists.
A lot of computations involve processing a string one character at a time. Often they start at the beginning, select each
character in turn, do something to it, and continue until the end. This pattern of processing is called a traversal. One
way (a very bad way) to encode a traversal is with a while statement:
1 ix = 0
2 while ix < len(fruit):
3 letter = fruit[ix]
4 print(letter)
5 ix += 1
This loop traverses the string and displays each letter on a line by itself. It uses ix for the index, which does not
make it any clearer. The loop condition is ix < len(fruit), so when ix is equal to the length of the string,
the condition is false, and the body of the loop is not executed. The last character accessed is the one with the index
len(fruit)-1, which is the last character in the string. However, this code is a lot longer than it needs to be, and
not very clear at all.
But we’ve previously seen how the for loop can easily iterate over the elements in a list and it can do so for strings
as well:
1 word="Banana"
2 for letter in word:
3 print(letter)
Each time through the loop, the next character in the string is assigned to the variable c. The loop continues until no
characters are left. Here we can see the expressive power the for loop gives us compared to the while loop when
traversing a string.
The following example shows how to use concatenation and a for loop to generate an abecedarian series. Abecedarian
refers to a series or list in which the elements appear in alphabetical order. For example, in Robert McCloskey’s book
Make Way for Ducklings, the names of the ducklings are Jack, Kack, Lack, Mack, Nack, Ouack, Pack, and Quack.
This loop outputs these names in order:
1 prefixes = "JKLMNOPQ"
2 suffix = "ack"
3
4 for p in prefixes:
5 print(p + suffix)
Jack
Kack
Lack
Mack
Nack
Oack
Pack
Qack
Of course, that’s not quite right because Ouack and Quack are misspelled. You’ll fix this as an exercise below.
5.1.6 Slices
A substring of a string is obtained by taking a slice. Similarly, we can slice a list to refer to some sublist of the items
in the list:
The operator [n:m] returns the part of the string from the n’th character to the m’th character, including the first but
excluding the last. This behavior makes sense if you imagine the indices pointing between the characters, as in the
following diagram:
If you imagine this as a piece of paper, the slice operator [n:m] copies out the part of the paper between the n and m
positions. Provided m and n are both within the bounds of the string, your result will be of length (m-n).
Three tricks are added to this: if you omit the first index (before the colon), the slice starts at the beginning of the
string (or list). If you omit the second index, the slice extends to the end of the string (or list). Similarly, if you provide
value for n that is bigger than the length of the string (or list), the slice will take all the values up to the end. (It won’t
give an “out of range” error like the normal indexing operation does.) Thus:
5.1. Strings 95
How to Think Like a Computer Scientist: Learning with Python 3 Documentation, Release 3rd
Edition
The comparison operators work on strings. To see if two strings are equal:
1 if word == "banana":
2 print("Yes, we have no bananas!")
Other comparison operations are useful for putting words in lexicographical order:
1 if word < "banana":
2 print("Your word, " + word + ", comes before banana.")
3 elif word > "banana":
4 print("Your word, " + word + ", comes after banana.")
5 else:
6 print("Yes, we have no bananas!")
This is similar to the alphabetical order you would use with a dictionary, except that all the uppercase letters come
before all the lowercase letters. As a result:
Your word, Zebra, comes before banana.
A common way to address this problem is to convert strings to a standard format, such as all lowercase, before
performing the comparison. A more difficult problem is making the program realize that zebras are not fruit.
It is tempting to use the [] operator on the left side of an assignment, with the intention of changing a character in a
string. For example:
1 greeting = "Hello, world!"
2 greeting[0] = 'J' # ERROR!
3 print(greeting)
Instead of producing the output Jello, world!, this code produces the runtime error TypeError: 'str'
object does not support item assignment.
Strings are immutable, which means you can’t change an existing string. The best you can do is create a new string
that is a variation on the original:
1 greeting = "Hello, world!"
2 new_greeting = "J" + greeting[1:]
3 print(new_greeting)
The solution here is to concatenate a new first letter onto a slice of greeting. This operation has no effect on the
original string.
The in operator tests for membership. When both of the arguments to in are strings, in checks whether the left
argument is a substring of the right argument.
>>> "p" in "apple"
True
>>> "i" in "apple"
False
(continues on next page)
Note that a string is a substring of itself, and the empty string is a substring of any other string. (Also note that
computer scientists like to think about these edge cases quite carefully!)
Combining the in operator with string concatenation using +, we can write a function that removes all the vowels
from a string:
1 def remove_vowels(phrase):
2 vowels = "aeiou"
3 string_sans_vowels = ""
4 for letter in phrase:
5 if letter.lower() not in vowels:
6 string_sans_vowels += letter
7 return string_sans_vowels
Important to note is the letter.lower() in line 5, without it, any uppercase vowels would not be removed.
Compare the output of the code above with what Python does itself with the code below:
1 haystack = "Bananarama!"
2 print(haystack.find('a'))
3 print(my_find(haystack,'a'))
5.1. Strings 97
How to Think Like a Computer Scientist: Learning with Python 3 Documentation, Release 3rd
Edition
In a sense, find is the opposite of the indexing operator. Instead of taking an index and extracting the corresponding
character, it takes a character and finds the index where that character appears. If the character is not found, the
function returns -1.
This is another example where we see a return statement inside a loop. If letter == needle, the function
returns immediately, breaking out of the loop prematurely.
If the character doesn’t appear in the string, then the program exits the loop normally and returns -1.
This pattern of computation is sometimes called a eureka traversal or short-circuit evaluation, because as soon as
we find what we are looking for, we can cry “Eureka!”, take the short-circuit, and stop looking.
The following program counts the number of times the letter a appears in a string, and is another example of the
counter pattern introduced in Counting digits:
1 def count_a(text):
2 count = 0
3 for letter in text:
4 if letter == "a":
5 count += 1
6 return(count)
7
8 print(count_a("banana") == 3)
To find the locations of the second or third occurrence of a character in a string, we can modify the find function,
adding a third parameter for the starting position in the search string:
9 print(find2("banana", "a", 2) == 3)
The call find2("banana", "a", 2) now returns 3, the index of the first occurrence of “a” in “banana” starting
the search at index 2. What does find2("banana", "n", 3) return? If you said, 4, there is a good chance you
understand how find2 works.
Better still, we can combine find and find2 using an optional parameter:
When a function has an optional parameter, the caller may provide a matching argument. If the third argument is
provided to find, it gets assigned to start. But if the caller leaves the argument out, then start is given a default
value indicated by the assignment start=0 in the function definition.
So the call find("banana", "a", 2) to this version of find behaves just like find2, while in the call
find("banana", "a"), start will be set to the default value of 0.
Adding another optional parameter to find makes it search from a starting position, up to but not including the end
position:
The semantics of start and end in this function are precisely the same as they are in the range function.
Now that we’ve done all this work to write a powerful find function, we can reveal that strings already have their
own built-in find method. It can do everything that our code can do, and more! Try all the examples listed above,
and check the results!
The built-in find method is more general than our version. It can find substrings, not just single characters:
>>> "banana".find("nan")
2
>>> "banana".find("na", 3)
4
Usually we’d prefer to use the methods that Python provides rather than reinvent our own equivalents. But many of
the built-in functions and methods make good teaching exercises, and the underlying techniques you learn are your
building blocks to becoming a proficient programmer.
One of the most useful methods on strings is the split method: it splits a single multi-word string into a list of
individual words, removing all the whitespace between them. (Whitespace means any tabs, newlines, or spaces.) This
allows us to read input as a single string, and split it into words.
We’ll often work with strings that contain punctuation, or tab and newline characters, especially, as we’ll see in a
future chapter, when we read our text from files or from the Internet. But if we’re writing a program, say, to count
word frequencies or check the spelling of each word, we’d prefer to strip off these unwanted characters.
We’ll show just one example of how to strip punctuation from a string. Remember that strings are immutable, so
we cannot change the string with the punctuation — we need to traverse the original string and create a new string,
omitting any punctuation:
5.1. Strings 99
How to Think Like a Computer Scientist: Learning with Python 3 Documentation, Release 3rd
Edition
1 punctuation = "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~"
2
3 def remove_punctuation(phrase):
4 phrase_sans_punct = ""
5 for letter in phrase:
6 if letter not in punctuation:
7 phrase_sans_punct += letter
8 return phrase_sans_punct
Setting up that first assignment is messy and error-prone. Fortunately, the Python string module already does it for
us. So we will make a slight improvement to this program — we’ll import the string module and use its definition:
1 import string
2
3 def remove_punctuation(phrase):
4 phrase_sans_punct = ""
5 for letter in phrase:
6 if letter not in string.punctuation:
7 phrase_sans_punct += letter
8 return phrase_sans_punct
Try the examples below: “Well, I never did!”, said Alice. “Are you very, very, sure?”
Composing together this function and the split method from the previous section makes a useful combination —
we’ll clean out the punctuation, and split will clean out the newlines and tabs while turning the string into a list of
words:
1 my_story = """
2 Pythons are constrictors, which means that they will 'squeeze' the life
3 out of their prey. They coil themselves around their prey and with
4 each breath the creature takes the snake will squeeze a little tighter
5 until they stop breathing completely. Once the heart stops the prey
6 is swallowed whole. The entire animal is digested in the snake's
7 stomach except for fur or feathers. What do you think happens to the fur,
8 feathers, beaks, and eggshells? The 'extra stuff' gets passed out as ---
9 you guessed it --- snake POOP! """
10
11 words = remove_punctuation(my_story).split()
12 print(words)
The output:
['Pythons', 'are', 'constrictors', ... , 'it', 'snake', 'POOP']
There are other useful string methods, but this book isn’t intended to be a reference manual. On the other hand, the
Python Library Reference is. Along with a wealth of other documentation, it is available at the Python website.
The easiest and most powerful way to format a string in Python 3 is to use the format method. To see how this
works, let’s start with a few examples:
1 phrase = "His name is {0}!".format("Arthur")
2 print(phrase)
3
4 name = "Alice"
(continues on next page)
11 x = 4
12 y = 5
13 phrase = "2**10 = {0} and {1} * {2} = {3:f}".format(2**10, x, y, x * y)
14 print(phrase)
The template string contains place holders, ... {0} ... {1} ... {2} ... etc. The format method substi-
tutes its arguments into the place holders. The numbers in the place holders are indexes that determine which argument
gets substituted — make sure you understand line 6 above!
But there’s more! Each of the replacement fields can also contain a format specification — it is always introduced
by the : symbol (Line 13 above uses one.) This modifies how the substitutions are made into the template, and can
control things like:
• whether the field is aligned to the left <, center ^, or right >
• the width allocated to the field within the result string (a number like 10)
• the type of conversion (we’ll initially only force conversion to float, f, as we did in line 13 of the code above,
or perhaps we’ll ask integer numbers to be converted to hexadecimal using x)
• if the type conversion is a float, you can also specify how many decimal places are wanted (typically, .2f is
useful for working with currencies to two decimal places.)
Let’s do a few simple and common examples that should be enough for most needs. If you need to do anything more
esoteric, use help and read all the powerful, gory details.
1 name1 = "Paris"
2 name2 = "Whitney"
3 name3 = "Hilton"
4
You can have multiple placeholders indexing the same argument, or perhaps even have extra arguments that are not
referenced at all:
1 letter = """
2 Dear {0} {2}.
3 {0}, I have an interesting money-making proposition for you!
4 If you deposit $10 million into my bank account, I can
5 double your money ...
6 """
7
As you might expect, you’ll get an index error if your placeholders refer to arguments that you do not provide:
>>> "hello {3}".format("Dave")
Traceback (most recent call last):
File "<interactive input>", line 1, in <module>
IndexError: tuple index out of range
The following example illustrates the real utility of string formatting. First, we’ll try to print a table without using
string formatting:
1 print("i\ti**2\ti**3\ti**5\ti**10\ti**20")
2 for i in range(1, 11):
3 print(i, "\t", i**2, "\t", i**3, "\t", i**5, "\t",
4 i**10, "\t", i**20)
This program prints out a table of various powers of the numbers from 1 to 10. (This assumes that the tab width is
8. You might see something even worse than this if you tab width is set to 4.) In its current form it relies on the tab
character ( \t) to align the columns of values, but this breaks down when the values in the table get larger than the tab
width:
i i**2 i**3 i**5 i**10 i**20
1 1 1 1 1 1
2 4 8 32 1024 1048576
3 9 27 243 59049 3486784401
4 16 64 1024 1048576 1099511627776
5 25 125 3125 9765625 95367431640625
6 36 216 7776 60466176 3656158440062976
7 49 343 16807 282475249 79792266297612001
8 64 512 32768 1073741824 1152921504606846976
9 81 729 59049 3486784401 12157665459056928801
10 100 1000 100000 10000000000 100000000000000000000
One possible solution would be to change the tab width, but the first column already has more space than it needs.
The best solution would be to set the width of each column independently. As you may have guessed by now, string
formatting provides a much nicer solution. We can also right-justify each field:
1 layout = "{0:>4}{1:>6}{2:>6}{3:>8}{4:>13}{5:>24}"
2
Running this version produces the following (much more satisfying) output:
5.1.17 Summary
This chapter introduced a lot of new ideas. The following summary may prove helpful in remembering what you
learned.
indexing ([]) Access a single character in a string using its position (starting from 0). Example: "This"[2]
evaluates to "i".
length function (len) Returns the number of characters in a string. Example: len("happy") evaluates to 5.
for loop traversal (for) Traversing a string means accessing each character in the string, one at a time. For example,
the following for loop:
for ch in "Example":
...
executes the body of the loop 7 times with different values of ch each time.
slicing ([:]) A slice is a substring of a string. Example: 'bananas and cream'[3:6] evaluates to ana (so
does 'bananas and cream'[1:4]).
string comparison (>, <, >=, <=, ==, !=) The six common comparison operators work with strings, evalu-
ating according to lexicographical order. Examples: "apple" < "banana" evaluates to True. "Zeta"
< "Appricot" evaluates to False. "Zebra" <= "aardvark" evaluates to True because all upper
case letters precede lower case letters.
in and not in operator (in, not in) The in operator tests for membership. In the case of strings, it tests whether
one string is contained inside another string. Examples: "heck" in "I'll be checking for you."
evaluates to True. "cheese" in "I'll be checking for you." evaluates to False.
5.1.18 Glossary
compound data type A data type in which the values are made up of components, or elements, that are themselves
values.
default value The value given to an optional parameter if no argument for it is provided in the function call.
docstring A string constant on the first line of a function or module definition (and as we will see later, in class
and method definitions as well). Docstrings provide a convenient way to associate documentation with code.
Docstrings are also used by programming tools to provide interactive help.
dot notation Use of the dot operator, ., to access methods and attributes of an object.
immutable data value A data value which cannot be modified. Assignments to elements or slices (sub-parts) of
immutable values cause a runtime error.
index A variable or value used to select a member of an ordered collection, such as a character from a string, or an
element from a list.
mutable data value A data value which can be modified. The types of all mutable values are compound types. Lists
and dictionaries are mutable; strings and tuples are not.
optional parameter A parameter written in a function header with an assignment to a default value which it will
receive if no corresponding argument is given for it in the function call.
short-circuit evaluation A style of programming that shortcuts extra work as soon as the outcome is know with
certainty. In this chapter our find function returned as soon as it found what it was looking for; it didn’t
traverse all the rest of the items in the string.
slice A part of a string (substring) specified by a range of indices. More generally, a subsequence of any sequence
type in Python can be created using the slice operator (sequence[start:stop]).
traverse To iterate through the elements of a collection, performing a similar operation on each.
whitespace Any of the characters that move the cursor without printing visible characters. The constant string.
whitespace contains all the white-space characters.
5.1.19 Exercises
>>> "Python"[1]
>>> "Strings are sequences of characters."[5]
>>> len("wonderful")
>>> "Mystery"[:4]
>>> "p" in "Pineapple"
>>> "apple" in "Pineapple"
>>> "pear" not in "Pineapple"
>>> "apple" > "pineapple"
>>> "pineapple" < "Peach"
2. Modify:
1 prefixes = "JKLMNOPQ"
2 suffix = "ack"
3
1 word = "banana"
2 count = 0
3 for letter in word:
(continues on next page)
in a function named count_letters, and generalize it so that it accepts the string and the letter as arguments.
Make the function return the number of characters, rather than print the answer. The caller should do the printing.
4. Now rewrite the count_letters function so that instead of traversing the string, it repeatedly calls the find
method, with the optional third parameter to locate new occurrences of the letter being counted.
5. Assign to a variable in your program a triple-quoted string that contains your favourite paragraph of text —
perhaps a poem, a speech, instructions to bake a cake, some inspirational verses, etc.
Write a function which removes all punctuation from the string, breaks the string into a list of words, and counts
the number of words in your text that contain the letter “e”. Your program should print an analysis of the text
like this:
Your text contains 243 words, of which 109 (44.8%) contain an "e".
7. Write a function that reverses its string argument, and satisfies these tests:
1 reverse("happy") == "yppah"
2 reverse("Python") == "nohtyP"
3 reverse("") == ""
4 reverse("a") == "a"
9. Write a function that removes all occurrences of a given letter from a string:
1 remove_letter("a", "apple") == "pple"
2 remove_letter("a", "banana") == "bnn"
3 remove_letter("z", "banana") == "banana"
4 remove_letter("i", "Mississippi") == "Msssspp"
5 remove_letter("b", "") = ""
6 remove_letter("b", "c") = "c"
10. Write a function that recognizes palindromes. (Hint: use your reverse function to make this easy!):
1 is_palindrome("abba")
2 not is_palindrome("abab")
3 is_palindrome("tenet")
4 not is_palindrome("banana")
5 is_palindrome("straw warts")
6 is_palindrome("a")
7 # is_palindrome("")) # Is an empty string a palindrome?
11. Write a function that counts how many times a substring occurs in a string:
1 count("is", "Mississippi") == 2
2 count("an", "banana") == 2
3 count("ana", "banana") == 2
4 count("nana", "banana") == 1
5 count("nanan", "banana") == 0
6 count("aaa", "aaaaaa") == 4
12. Write a function that removes the first occurrence of a string from another string:
1 remove("an", "banana") == "bana"
2 remove("cyc", "bicycle") == "bile"
3 remove("iss", "Mississippi") == "Missippi"
4 remove("eggs", "bicycle") == "bicycle"
13. Write a function that removes all occurrences of a string from another string:
1 remove_all("an", "banana") == "ba"
2 remove_all("cyc", "bicycle") == "bile"
3 remove_all("iss", "Mississippi") == "Mippi"
4 remove_all("eggs", "bicycle") == "bicycle"
There are only four really important operations on strings, and we’ll be able to do just about anything. There are many
more nice-to-have methods (we’ll call them sugar coating) that can make life easier, but if we can work with the basic
four operations smoothly, we’ll have a great grounding.
• len(str) finds the length of a string.
• str[i] the subscript operation extracts the i’th character of the string, as a new string.
• str[i:j] the slice operation extracts a substring out of a string.
• str.find(target) returns the index where target occurs within the string, or -1 if it is not found.
So if we need to know if “snake” occurs as a substring within s, we could write
1 if s.find("snake") >= 0: ...
2 if "snake" in s: ... # Also works, nice-to-know sugar coating!
It would be wrong to split the string into words unless we were asked whether the word “snake” occurred in the string.
Suppose we’re asked to read some lines of data and find function definitions, e.g.: def
some_function_name(x, y):, and we are further asked to isolate and work with the name of the func-
tion. (Let’s say, print it.)
1 s = "..." # Get the next line from somewhere
2 def_pos = s.find("def ") # Look for "def " in the line
3 if def_pos == 0: # If it occurs at the left margin
4 op_index = s.find("(") # Find the index of the open parenthesis
(continues on next page)
5.2 Tuples
We saw earlier that we could group together pairs of values by surrounding with parentheses. Recall this example:
This is an example of a data structure — a mechanism for grouping and organizing data to make it easier to use.
The pair is an example of a tuple. Generalizing this, a tuple can be used to group any number of items into a single
compound value. Syntactically, a tuple is a comma-separated sequence of values. Although it is not necessary, it is
conventional to enclose tuples in parentheses:
The other thing that could be said somewhere around here, is that the parentheses are there to disambiguate. For
example, if we have a tuple nested within another tuple and the parentheses weren’t there, how would we tell where
the nested tuple begins/ends? Also: the creation of an empty tuple is done like this: empty_tuple=()
Tuples are useful for representing what other languages often call records (or structs) — some related information
that belongs together, like your student record. There is no description of what each of these fields means, but we can
guess. A tuple lets us “chunk” together related information and use it as a single thing.
Tuples support the same sequence operations as strings. The index operator selects an element from a tuple.
>>> julia[2]
1967
But if we try to use item assignment to modify one of the elements of the tuple, we get an error:
>>> julia[0] = "X"
TypeError: 'tuple' object does not support item assignment
So like strings, tuples are immutable. Once Python has created a tuple in memory, it cannot be changed.
Of course, even if we can’t modify the elements of a tuple, we can always make the julia variable reference a new
tuple holding different information. To construct the new tuple, it is convenient that we can slice parts of the old
tuple and join up the bits to make the new tuple. So if julia has a new recent film, we could change her variable to
reference a new tuple that used some information from the old one:
>>> julia = julia[:3] + ("Eat Pray Love", 2010) + julia[5:]
>>> julia
("Julia", "Roberts", 1967, "Eat Pray Love", 2010, "Actress", "Atlanta,
˓→Georgia")
To create a tuple with a single element (but you’re probably not likely to do that too often), we have to include the final
comma, because without the final comma, Python treats the (5) below as an integer in parentheses:
>>> tup = (5,)
>>> type(tup)
<class 'tuple'>
>>> x = (5)
>>> type(x)
<class 'int'>
Python has a very powerful tuple assignment feature that allows a tuple of variables on the left of an assignment to
be assigned values from a tuple on the right of the assignment. (We already saw this used for pairs, but it generalizes.)
(name, surname, year_born, movie, year_movie, profession, birthplace) = julia
This does the equivalent of seven assignment statements, all on one easy line. One requirement is that the number of
variables on the left must match the number of elements in the tuple.
One way to think of tuple assignment is as tuple packing/unpacking.
In tuple packing, the values on the left are ‘packed’ together in a tuple:
>>> bob = ("Bob", 19, "CS") # tuple packing
In tuple unpacking, the values in a tuple on the right are ‘unpacked’ into the variables/names on the right:
>>> bob = ("Bob", 19, "CS")
>>> (name, age, studies) = bob # tuple unpacking
>>> name
(continues on next page)
Once in a while, it is useful to swap the values of two variables. With conventional assignment statements, we have to
use a temporary variable. For example, to swap a and b:
1 temp = a
2 a = b
3 b = temp
1 (a, b) = (b, a)
The left side is a tuple of variables; the right side is a tuple of values. Each value is assigned to its respective variable.
All the expressions on the right side are evaluated before any of the assignments. This feature makes tuple assignment
quite versatile.
Naturally, the number of variables on the left and the number of values on the right have to be the same:
Functions can always only return a single value, but by making that value a tuple, we can effectively group together
as many values as we like, and return them together. This is very useful — we often want to know some batsman’s
highest and lowest score, or we want to find the mean and the standard deviation, or we want to know the year, the
month, and the day, or if we’re doing some some ecological modelling we may want to know the number of rabbits
and the number of wolves on an island at a given time.
For example, we could write a function that returns both the area and the circumference of a circle of radius r:
1 def circle_stats(r):
2 """ Return (circumference, area) of a circle of radius r """
3 circumference = 2 * math.pi * r
4 area = math.pi * r * r
5 return (circumference, area)
We saw in an earlier chapter that we could make a list of pairs, and we had an example where one of the items in the
tuple was itself a list:
students = [
("John", ["CompSci", "Physics"]),
("Vusi", ["Maths", "CompSci", "Stats"]),
("Jess", ["CompSci", "Accounting", "Economics", "Management"]),
("Sarah", ["InfSys", "Accounting", "Economics", "CommLaw"]),
("Zuki", ["Sociology", "Economics", "Law", "Stats", "Music"])]
Tuples items can themselves be other tuples. For example, we could improve the information about our movie stars to
hold the full date of birth rather than just the year, and we could have a list of some of her movies and dates that they
were made, and so on:
Notice in this case that the tuple has just five elements — but each of those in turn can be another tuple, a list, a string,
or any other kind of Python value. This property is known as being heterogeneous, meaning that it can be composed
of elements of different types.
5.2.5 Glossary
data structure An organization of data for the purpose of making it easier to use.
immutable data value A data value which cannot be modified. Assignments to elements or slices (sub-parts) of
immutable values cause a runtime error.
mutable data value A data value which can be modified. The types of all mutable values are compound types. Lists
and dictionaries are mutable; strings and tuples are not.
tuple An immutable data value that contains related elements. Tuples are used to group together related data, such as
a person’s name, their age, and their gender.
tuple assignment An assignment to all of the elements in a tuple using a single assignment statement. Tuple assign-
ment occurs simultaneously rather than in sequence, making it useful for swapping values.
5.2.6 Exercises
1. We’ve said nothing in this chapter about whether you can pass tuples as arguments to a function. Construct a
small Python example to test whether this is possible, and write up your findings.
2. Is a pair a generalization of a tuple, or is a tuple a generalization of a pair?
3. Is a pair a kind of tuple, or is a tuple a kind of pair?
5.3 Lists
A list is an ordered collection of values. The values that make up a list are called its elements, or its items. We will
use the term element or item to mean the same thing. Lists are similar to strings, which are ordered collections of
characters, except that the elements of a list can be of any type. Lists and strings — and other collections that maintain
the order of their items — are called sequences.
There are several ways to create a new list; the simplest is to enclose the elements in square brackets ([ and ]):
The first example is a list of four integers. The second is a list of three strings. The elements of a list don’t have to be
the same type. The following list contains a string, a float, an integer, and (amazingly) another list:
The syntax for accessing the elements of a list is the same as the syntax for accessing the characters of a string — the
index operator: [] (not to be confused with an empty list). The expression inside the brackets specifies the index.
Remember that the indices start at 0:
>>> numbers[0]
17
>>> numbers[9-8]
123
>>> numbers[1.0]
Traceback (most recent call last):
File "<interactive input>", line 1, in <module>
TypeError: list indices must be integers, not float
If you try to access or assign to an element that does not exist, you get a runtime error:
>>> numbers[2]
Traceback (most recent call last):
File "<interactive input>", line 1, in <module>
IndexError: list index out of range
Each time through the loop, the variable i is used as an index into the list, printing the i’th element. This pattern of
computation is called a list traversal.
The above sample doesn’t need or use the index i for anything besides getting the items from the list, so this more
direct version — where the for loop gets the items — is much more clear!
3 for h in horsemen:
4 print(h)
The function len returns the length of a list, which is equal to the number of its elements. If you are going to use an
integer index to access the list, it is a good idea to use this value as the upper bound of a loop instead of a constant.
That way, if the size of the list changes, you won’t have to go through the program changing all the loops; they will
work correctly for any size list:
3 for i in range(len(horsemen)):
4 print(horsemen[i])
The last time the body of the loop is executed, i is len(horsemen) - 1, which is the index of the last element.
(But the version without the index looks even better now! The version above is not the right way to do things!)
Although a list can contain another list, the nested list still counts as a single element in its parent list. The length of
this list is 4:
in and not in are Boolean operators that test membership in a sequence. We used them previously with strings, but
they also work with lists and other sequences:
Using this produces a more elegant version of the nested loop program we previously used to count the number of
students doing Computer Science in the section Nested Loops for Nested Data:
1 students = [
2 ("John", ["CompSci", "Physics"]),
3 ("Vusi", ["Maths", "CompSci", "Stats"]),
4 ("Jess", ["CompSci", "Accounting", "Economics", "Management"]),
5 ("Sarah", ["InfSys", "Accounting", "Economics", "CommLaw"]),
6 ("Zuki", ["Sociology", "Economics", "Law", "Stats", "Music"])]
(continues on next page)
>>> [0] * 4
[0, 0, 0, 0]
>>> [1, 2, 3] * 3
[1, 2, 3, 1, 2, 3, 1, 2, 3]
The first example repeats [0] four times. The second example repeats the list [1, 2, 3] three times.
The slice operations we saw previously with strings let us work with sublists:
Unlike strings, lists are mutable, which means we can change their elements. Using the index operator on the left side
of an assignment, we can update one of the elements:
The bracket operator applied to a list can appear anywhere in an expression. When it appears on the left side of
an assignment, it changes one of the elements in the list, so the first element of fruit has been changed from
"banana" to "pear", and the last from "quince" to "orange". An assignment to an element of a list is called
item assignment. Item assignment does not work for strings:
We can also remove elements from a list by assigning an empty list to them:
And we can add elements to a list by squeezing them into an empty slice at the desired location:
Using slices to delete list elements can be error-prone. Python provides an alternative that is more readable. The del
statement removes an element from a list:
As you might expect, del causes a runtime error if the index is out of range.
You can also use del with a slice to delete a sublist:
As usual, the sublist selected by slice contains all the elements up to, but not including, the second index.
1 a = "banana"
2 b = "banana"
we know that a and b will refer to a string object with the letters "banana". But we don’t know yet whether they
point to the same string object.
There are two possible ways the Python interpreter could arrange its memory:
In one case, a and b refer to two different objects that have the same value. In the second case, they refer to the same
object.
We can test whether two names refer to the same object using the is operator:
>>> a is b
True
This tells us that both a and b refer to the same object, and that it is the second of the two state snapshots that accurately
describes the relationship.
Since strings are immutable, Python optimizes resources by making two names that refer to the same string value refer
to the same object.
This is not the case with lists:
>>> a = [1, 2, 3]
>>> b = [1, 2, 3]
>>> a == b
True
>>> a is b
False
a and b have the same value but do not refer to the same object.
5.3.10 Aliasing
Since variables refer to objects, if we assign one variable to another, both variables refer to the same object:
>>> a = [1, 2, 3]
>>> b = a
>>> a is b
True
Because the same list has two different names, a and b, we say that it is aliased. Changes made with one alias affect
the other:
>>> b[0] = 5
>>> a
[5, 2, 3]
Although this behavior can be useful, it is sometimes unexpected or undesirable. In general, it is safer to avoid aliasing
when you are working with mutable objects (i.e. lists at this point in our textbook, but we’ll meet more mutable objects
as we cover classes and objects, dictionaries and sets). Of course, for immutable objects (i.e. strings, tuples), there’s
no problem — it is just not possible to change something and get a surprise when you access an alias name. That’s
why Python is free to alias strings (and any other immutable kinds of data) when it sees an opportunity to economize.
If we want to modify a list and also keep a copy of the original, we need to be able to make a copy of the list itself, not
just the reference. This process is sometimes called cloning, to avoid the ambiguity of the word copy.
The easiest way to clone a list is to use the slice operator:
>>> a = [1, 2, 3]
>>> b = a[:]
>>> b
[1, 2, 3]
Taking any slice of a creates a new list. In this case the slice happens to consist of the whole list. So now the
relationship is like this:
Now we are free to make changes to b without worrying that we’ll inadvertently be changing a:
>>> b[0] = 5
>>> a
[1, 2, 3]
The for loop also works with lists, as we’ve already seen. The generalized syntax of a for loop is:
It almost reads like English: For (every) friend in (the list of) friends, print (the name of the) friend.
Any list expression can be used in a for loop:
The first example prints all the multiples of 3 between 0 and 19. The second example expresses enthusiasm for various
fruits.
Since lists are mutable, we often want to traverse a list, changing each of its elements. The following squares all the
numbers in the list xs:
1 xs = [1, 2, 3, 4, 5]
2
3 for i in range(len(xs)):
4 xs[i] = xs[i]**2
Take a moment to think about range(len(xs)) until you understand how it works.
In this example we are interested in both the value of an item, (we want to square that value), and its index (so that
we can assign the new value to that position). This pattern is common enough that Python provides a nicer way to
implement it:
1 xs = [1, 2, 3, 4, 5]
2
enumerate generates pairs of both (index, value) during the list traversal. Try this next example to see more clearly
how enumerate works:
0 banana
1 apple
2 pear
3 lemon
Passing a list as an argument actually passes a reference to the list, not a copy or clone of the list. So parameter passing
creates an alias for you: the caller has one variable referencing the list, and the called function has an alias, but there
is only one underlying list object. For example, the function below takes a list as an argument and multiplies each
element in the list by 2:
1 def double_stuff(stuff_list):
2 """ Overwrite each element in a_list with double its value. """
3 for (index, stuff) in enumerate(stuff_list):
4 stuff_list[index] = 2 * stuff
1 things = [2, 5, 9]
2 double_stuff(things)
3 print(things)
In the function above, the parameter stuff_list and the variable things are aliases for the same object. So
before any changes to the elements in the list, the state snapshot looks like this:
Since the list object is shared by two frames, we drew it between them.
If a function modifies the items of a list parameter, the caller sees the change.
The dot operator can also be used to access built-in methods of list objects. We’ll start with the most useful method
for adding something onto the end of an existing list:
>>> mylist = []
>>> mylist.append(5)
>>> mylist.append(27)
>>> mylist.append(3)
>>> mylist.append(12)
>>> mylist
[5, 27, 3, 12]
append is a list method which adds the argument passed to it to the end of the list. We’ll use it heavily when we’re
creating new lists. Continuing with this example, we show several other list methods:
Experiment and play with the list methods shown here, and read their documentation until you feel confident that you
understand how they work.
As seen before, there is a difference between a pure function and one with side-effects. The difference is shown below
as lists have some special gotcha’s. Functions which take lists as arguments and change them during execution are
called modifiers and the changes they make are called side effects.
A pure function does not produce side effects. It communicates with the calling program only through parameters,
which it does not modify, and a return value. Here is double_stuff written as a pure function:
1 def double_stuff(a_list):
2 """ Return a new list which contains
3 doubles of the elements in a_list.
4 """
5 new_list = []
6 for value in a_list:
7 new_elem = 2 * value
8 new_list.append(new_elem)
9
10 return new_list
An early rule we saw for assignment said “first evaluate the right hand side, then assign the resulting value to the
variable”. So it is quite safe to assign the function result to the same variable that was passed to the function:
The pure version of double_stuff above made use of an important pattern for your toolbox. Whenever you need
to write a function that creates and returns a list, the pattern is usually:
Let us show another use of this pattern. Assume you already have a function is_prime(x) that can test if x is
prime. Write a function to return a list of all prime numbers less than n:
1 def primes_lessthan(n):
2 """ Return a list of all prime numbers less than n. """
3 result = []
4 for i in range(2, n):
5 if is_prime(i):
6 result.append(i)
7 return result
Two of the most useful methods on strings involve conversion to and from lists of substrings. The split method
(which we’ve already seen) breaks a string into a list of words. By default, any number of whitespace characters is
considered a word boundary:
An optional argument called a delimiter can be used to specify which string to use as the boundary marker between
substrings. The following example uses the string ai as the delimiter:
>>> song.split("ai")
['The r', 'n in Sp', 'n...']
The list that you glue together (words in this example) is not modified. Also, as these next examples show, you can
use empty glue or multi-character strings as glue:
Python has a built-in type conversion function called list that tries to turn whatever you give it into a list.
One particular feature of range is that it doesn’t instantly compute all its values: it “puts off” the computation, and
does it on demand, or “lazily”. We’ll say that it gives a promise to produce the values when they are needed. This is
very convenient if your computation short-circuits a search and returns early, as in this case:
1 def f(n):
2 """ Find the first positive integer between 101 and less
3 than n that is divisible by 21
4 """
5 for i in range(101, n):
6 if (i % 21 == 0):
7 return i
8
10 print(f(110) == 105)
11 print(f(1000000000) == 105)
In the second test, if range were to eagerly go about building a list with all those elements, you would soon exhaust
your computer’s available memory and crash the program. But it is cleverer than that! This computation works just
fine, because the range object is just a promise to produce the elements if and when they are needed. Once the
condition in the if becomes true, no further elements are generated, and the function returns. (Note: Before Python
3, range was not lazy. If you use an earlier versions of Python, YMMV!)
You’ll sometimes find the lazy range wrapped in a call to list. This forces Python to turn the lazy promise into an
actual list:
Computers are useful because they can repeat computation, accurately and fast. So loops are going to be a central
feature of almost all programs you encounter.
Lists are useful if you need to keep data for later computation. But if you don’t need lists, it is probably better not to
generate them.
Here are two functions that both generate ten million random numbers, and return the sum of the numbers. They both
work.
1 import random
2 joe = random.Random()
3
4 def sum1():
5 """ Build a list of random numbers, then sum them """
6 xs = []
7 for i in range(10000000):
8 num = joe.randrange(1000) # Generate one random number
9 xs.append(num) # Save it in our list
10
11 tot = sum(xs)
12 return tot
13
14 def sum2():
15 """ Sum the random numbers as we generate them """
16 tot = 0
17 for i in range(10000000):
18 num = joe.randrange(1000)
19 tot += num
20 return tot
21
22 print(sum1())
23 print(sum2())
What reasons are there for preferring the second version here? (Hint: open a tool like the Performance Monitor on
your computer, and watch the memory usage. How big can you make the list before you get a fatal memory error in
sum1?)
In a similar way, when working with files, we often have an option to read the whole file contents into a single string,
or we can read one line at a time and process each line as we read it. Line-at-a-time is the more traditional and perhaps
safer way to do things — you’ll be able to work comfortably no matter how large the file is. (And, of course, this mode
of processing the files was essential in the old days when computer memories were much smaller.) But you may find
whole-file-at-once is sometimes more convenient!
A nested list is a list that appears as an element in another list. In this list, the element with index 3 is a nested list:
>>> print(nested[3])
[10, 20]
To extract an element from the nested list, we can proceed in two steps:
>>> nested[3][1]
20
Bracket operators evaluate from left to right, so this expression gets the 3’th element of nested and extracts the 1’th
element from it.
5.3.21 Matrices
Nested lists are often used to represent matrices. For example, the matrix:
mx is a list with three elements, where each element is a row of the matrix. We can select an entire row from the matrix
in the usual way:
>>> mx[1]
[4, 5, 6]
Or we can extract a single element from the matrix using the double-index form:
>>> mx[1][2]
6
The first index selects the row, and the second index selects the column. Although this way of representing matrices is
common, it is not the only possibility. A small variation is to use a list of columns instead of a list of rows. Later we
will see a more radical alternative using a dictionary.
5.3.22 Glossary
5.3.23 Exercises
The three arguments to the range function are start, stop, and step, respectively. In this example, start is
greater than stop. What happens if start < stop and step < 0? Write a rule for the relationships
among start, stop, and step.
2. Consider this fragment of code:
1 import turtle
2
3 tess = turtle.Turtle()
4 alex = tess
5 alex.color("hotpink")
Does this fragment create one or two turtle instances? Does setting the color of alex also change the color of
tess? Explain in detail.
3. Draw a state snapshot for a and b before and after the third line of the following Python code is executed:
1 a = [1, 2, 3]
2 b = a[:]
3 b[0] = 5