Lab: Working With Sequences
Preparation
For this lab, you need only use the REPL. Thus, you may immediately get started by opening up the Terminal
and typing python3
.
Exercise 1
We’ve discussed how strings and lists are sequences of characters. Is it possible to have an empty sequence?
a. Try typing type("")
into the REPL to see if it still qualifies as a string. What about type([])
?
b. What do you think len("")
or len([])
will evaluate to? Verify this in the REPL.
c. What do you think "" + "abc"
will evaluate to? Verify this in the REPL. What about [] + [1,2,3]
?
d. What about "" * 5
or [] * 5
? Verify this in the REPL.
The value ""
is called the empty string and []
is called the empty list. We will encounter them many times throughout the term.
Exercise 2
Certain characters do not have associated keys on the keyboard, and for convenience Python has special notation for specifying some of these characters. These are called escaped characters.
a. Try typing len("\n")
into the REPL. Is this what you expected?
b. Try typing print("hello\nworld")
into the REPL. What was the character \n
interpreted as?
c. Try typing print("Dear Friend,\n\nI am pleased to inform you that...")
into the REPL.
What if we actually want to have a backslash character in our string?
d. Try typing len("\\")
into the REPL.
e. Finally, try typing print("\\")
into the REPL.
Whenever we type a backslash \ before another character, it escapes that character to mean something else. If we escape a backslash by typing \\, this actually only produces a single \ character in our string.
Exercise 3
Type the following into the REPL:
>>> my_string = "equanimity"
>>> my_list = [1, "a", 2, "bc"]
(For the record, I found the word “equanimity” by searching for “long English words” in Google. This one caught my eye.)
a. Write an expression that extracts the "u"
character out of my_string
.
b. Write an expression that extracts the number 1
out of my_list
.
c. Write an expression that extracts the substring "equanimit"
out of my_string
. (i.e. just the last character is dropped)
d. Write an expression that extracts the substring ["a", 2, "bc"]
out of my_list
. (i.e. just the first element is dropped)
e. Write an expression that extracts the substring "equ"
out of my_string
.
f. Write an expression that extracts the substring "ity"
out of my_string
.
g. Write an expression that combines your expressions from e. and f. to produce "equity"
.
h. Write an expression that extracts the sublist ["a", 2]
out of my_list
.
Exercise 4
a. Assuming you still have my_string
defined, what do you think will happen when you evaluate the following expression into the REPL?
>>> my_string[len(my_string)]
b. What do you think will happen if you evaluate the above expression for different string values for my_string
? Can you make a general statement about what will happen for any string string value stored in my_string
? What about expressions like my_list[len(my_list)]
?
c. Evaluate the following two expressions and compare their results.
>>> my_string[len(my_string)-1]
>>> my_string[-1]
d. Evaluate the following two expressions and compare their results.
>>> my_list[len(my_string)-3]
>>> my_list[-3]
e. What do you think my_string[index]
or my_list[index]
is doing for negative values of index
? Discuss this with your partner and try it on other negative values such as -2
and -3
.
Exercise 6
We have seen that you can slice strings and lists with the syntax my_string[start:end]
. What do you think happens if you remove the start
or the end
index? Let’s try it.
First create a variable my_string = "abcdefg"
a. Try typing my_string[3:]
into the REPL.
b. Try typing my_string[:4]
into the REPL.
c. Experiment with leaving out start
or end
on a few more choices of indices and values of my_string
.
d. When you leave off the start
or end
index, Python assumes some default value. What is the default start
index? What is the default end
index?
e. Does this trick work for lists, too? Try it.
Exercise 7
Let’s experiment with the splitting strings. Open up a REPL and type the following command.
>>> sentence = "It is snowy outside."
a. What do you expect the result of sentence.split()
will be?
Since split
is called on a string object it is called a string method instead of a function. Some objects have associated methods that are called directly on the object itself. This is different from functions like round
that require the number to be passed in as a parameter such as round(5.78)
.
b. Try out the split
method with several other sentences of your choice.
c. What if you have a bunch of numbers stored in a string? For example, numbers = "54 75 31 10"
. Does split()
turn them into numbers for you? Verify your guess.
d. What if the data in your string is delimited by commas instead of spaces? Try using numbers = "54,75,31,10"
and then calling numbers.split(",")
.
e. What do you think the optional parameter to the split
method does? Does it work for strings other than ","
?
f. What do you think will happen if you call split
on the empty string?
g. What do you think will happen if you call split
on a string that is just full of spaces? For example, the string " ".split()
. What about if you evaluate "aaabbbbcc".split("bb")
?
h. For the extra curious human-beings, what if there are multiple ways to split the string? For example, see what happens if you evaluate "abababa".split("aba")
. (Try to avoid doing things like this! It is confusing, but beware!)
Exercise 8
Experiment with the join
method on strings. For example, predict what each of the following expressions will evaluate to, and then test them in the REPL.
>>> list_of_words = ["the", "quick", "brown", "fox"]
>>> ",".join(list_of_words)
>>> " ".join(list_of_words)
>>> "\n".join(list_of_words)
>>> ",".join(["spam"] * 5)
Try experimenting with other strings.
Exercise 9
In this exercise, we will explore the mutability of lists. Suppose we have the following commands executed in the REPL:
>>> lst1 = ["a", "b", "c"]
>>> lst2 = lst1
a. Try printing each of the lists by executing
>>> print(lst1)
>>> print(lst2)
b. What if we change one of the lists? For example:
>>> lst1[2] = "Hello, Bob."
Try printing the lists again after executing the above command.
c. What if we try using append
to increase the size of the list. Try executing.
>>> lst2.append(5)
Then try printing the contents of the lists again.
d. Experiment with appending more elements to the lists.
Notice that in the above expressions we only ever created one list which was in the lst1 = ["a", "b", "c"]
expression. All of the other expressions either causes lst2
to point to the same list or modifies the list directly.
e. Try doing the following:
>>> lst3 = lst1[1:3]
>>> lst2[1] = "Don't worry; be happy."
Do you think lst3
changes with the last expression? Try printing lst1
, lst2
, and lst3
again to check.