These exercises should be completed with your assigned partner. Both of you will take turns playing the role of the driver (the one currently screen sharing) and the navigator. Please refer to the course Code of Conduct for more details.
Before you begin, you should ensure that today you perform opposite roles as the previous lab.
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 shell to see if it still qualifies as a string.
b. What do you think
len("") will evaluate to? Verify this in the shell.
c. What do you think
"" + "abc" will evaluate to? Verify this in the shell.
d. What about
"" * 5? Verify this in the shell.
"" is called the empty string. We will encounter it many times throughout the term.
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 shell. Is this what you expected?
b. Try typing
print("hello\nworld") into the shell. What was the character
\n interpreted as?
c. Try typing
print("Dear Friend,\n\nI am pleased to inform you that...") into the shell.
Inside a string,
\n is represents a single character, in particular, the newline character. These are hard to express on your keyboard so you must specify them in a special way. The
\ character is used inside strings to specify these special types of characters.
d. What if we want to have a string that consists of a single
\ character? Try executing the following in the shell to see what happens:
e. Now try typing
len("\\") into the shell.
f. Finally, try typing
print("\\") into the shell.
Since the backslash
\ character is used in a special way, we must use
\\ to produce a single
\ character in our string.
It is easy to confuse the backslash character
\ (usually above the
Enter key on your keyboard) and the forwardslash character
/ (usually on the same key as the
? key). These are NOT the same character!
Type the following into the shell:
>>> s = "equanimity"
a. Start by typing in the following to get familiar with the indexing operation
>>> s >>> s
b. Using indexing, write an expression that extracts the
"u" character out of
c. Using slicing, write an expression that extracts the substring
"equanimit" out of
s. (i.e. just the last character is dropped)
d. Write an expression that extracts the substring
"equ" out of
e. Write an expression that extracts the substring
"ity" out of
f. Write an expression that combines your expressions from f. and g. to produce
"equity" using concatenation.
a. Assuming you still have
s defined, what do you think will happen when you evaluate the following expression in the shell? Discuss with your partner.
b. What do you think will happen if you evaluate the above expression for different string values for
s? Can you make a general statement about what will happen for any string string value stored in
s? Discuss with your partner.
c. Evaluate the following two expressions and compare their results.
>>> s[len(s)-1] >>> s[-1]
d. What is
s[index] doing for negative values of
index? Discuss this with your partner and try it on other negative values such as
It is common for us to need to access elements from the right-hand side of a sequence, so Python includes negative indexing so that we can more conveniently do this. Note that
s[-k] are equivalent for all positive indices
We have seen that you can slice strings with the syntax
s[start:end]. What do you think happens if you remove the
start or the
end index? Let’s try it.
First create a variable
s = "abcdefg"
a. Try typing
s[3:] into the shell.
b. Try typing
s[:4] into the shell.
c. Experiment with leaving out
end on a few more choices of indices and values of
d. When you leave off the
end index, Python assumes some default value. What is the default
start index? What is the default
Recall that we’ve seen the use of a
for loop to repeat an action numerous times. For example,
for i in range(10): print("Hello!")
will print the string
"Hello" ten times. However, this
for notation is doing something much more general than this.
a. Try typing the following into the shell:
range(10) defines a sequence of numbers, and here we converted that sequence to a list. This allows us to more easily see what numbers are in the sequence.
b. For each of the following expressions, discuss with your partner what list you think will be created and verify your prediction by typing it into the shell.
>>> list(range(5)) >>> list(range(10 * 2)) >>> list(range(12 // 2 + 10))
c. Create a new file called
definite_loops.py and copy/paste the following for-loop into it:
for i in range(10): print("Currently, i =", i) print("The square of i is", i ** 2)
d. Predict what you think will be printed and then run the program.
for-loop is iterating over the sequence
0, 1, 2, ..., 9. In fact, it reads nicely in English: “For each element, i, of 0, 1, 2, …, 9, print two things.”
e. Since a string is just a sequence of characters, we can also use
for-loops to iterate over strings. For example, try copying and pasting the following loop into your
sequence = "the quick brown fox" for ch in sequence: print("Currently, ch =", ch)
for-loops are really convenient but are not as general as
while loops. Copy and paste the following
while loop into your
sequence = "the quick brown fox" i = 0 while i < len(sequence): ch = sequence[i] print("Currently, ch =", ch) i = i + 1
g. Examine the
while loop with your partner and determine whether the loop does the same thing or something different than the previous
for-loop, then run the program to test your hypothesis.
If you have extra time, continue working on Assignment 5.