Exercise 1

We’ve discussed how lists are sequences of values. Is it possible to have an empty list?

a. Try typing type([]) into the REPL to see if it still qualifies as a list.

b. What do you think len([]) will evaluate to? Verify this in the REPL.

c. What do you think [] + [1,2,3] will evaluate to? Verify this in the REPL.

d. What about [] * 5? Verify this in the REPL.

The value [] is called the empty list and is similar to the empty string.

e. Can lists contain multiple types of values? Try creating lists that contain both strings and numbers.

Exercise 2

Type the following into the REPL. (For the record, I found this word by searching for “long English words” in Google. This one in particular caught my eye.)

>>> my_list = [1,2,3,4,5,6,7,8,9,10]

a. Write an expression that extracts the 3 out of my_list.

b. Write an expression that extracts the sublist [3,4,5] out of my_list.

c. Write an expression that extracts the [8,9,10] out of my_list.

d. Write an expression that combines your expressions from b. and c. to produce [3,4,5,8,9,10].

Exercise 3

Write a program string_to_ord.py that asks the user to input a string and then prints off the ordinals of the individual characters of that string separated by whitespace. Remember that you can turn a character into its ordinal number using the ord() function.

Also, remember that you can iterate over the individual characters of a string using a loop like the following:

for c in their_string:
    # do something here...

Exercise 4

Let’s experiment with the split() function. Open up a REPL and type the following command.

>>> sentence = "It is sunny 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.

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?

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 parameter to the split method does?

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().

Exercise 5

Create a file named sum_numbers.py that contains the following code and fill in the comment in the for-loop 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
numbers = input("Please enter a list of numbers separated by spaces: ")


total_sum = 

for num in numbers:
    # CODE NEEDS TO GO HERE

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

Exercise 6

Using the code from exercise 6 as a guide, implement a program average_numbers.py that asks the user to enter a list of numbers separated by spaces on a single line, and then prints off the average of those numbers.