Preparation

a. Make sure you are booted into the Mac OSX side of the computer.

b. Once you are logged in, double click the icon named courses.ads.carleton.edu on your desktop. If you encounter any errors, have me or the prefect contact Mike Tie to set up your account.

c. Open up a Terminal, navigate to your StuWork directory, create a folder named lab-01-21, and change your directory to the newly created folder. (Remember that you should replace USERNAME with your actual username!)

$ cd /Volumes/courses/cs111-01-w19/StuWork/USERNAME/
$ mkdir lab-01-21
$ cd lab-01-21

d. Open Brackets and make sure you have your StuWork/USERNAME directory opened. (You will see all of its subfolders in the left panel if so.) If the folder is not opened, you can open it by selecting File -> Open Folder and navigating to courses, then cs111-01-w19, then StuWork, then USERNAME.

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 output 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

Create a file named sum_numbers.py that contains the following code and fill in the comments to complete the program.

# sum_numbers.py
#
# Asks the user to enter a list of numbers separated by spaces on a single 
# line, and then prints off the sum of those numbers

def main():
    numbers = input("Please enter a list of numbers separated by spaces: ")

    list_of_numbers = # CODE NEEDS TO GO HERE

    total_sum = 0

    for number in list_of_numbers:
        # CODE NEEDS TO GO HERE

    print("The sum of all the numbers is:", total_sum)

if __name__ == "__main__":
    main()