CPSC120A
Fundamentals of Computer Science I

Activity 22

Lists.

Lists

Lists are similar to strings in that they sequentially store data. However, in strings the only data that can be contained in the sequence is a character. In lists, any data can be stored. The nice thing about lists is that the operations you can do on a list are very similar to the operations you can perform on a string (actually strings are based on lists, not vice versa). Today, you will explore what those operators are by completing the activities below.


Write how Python would evaluate each of the following expressions.

Expression Output
[1, 2, 3] + [4, 5, 6]
[1, 2, 3] - [2]
[1, 2, 3] / [2]
[1, 2, 3] * [3]
[1, 2, 3] + 4
[1, 2, 3] - 2
[1, 2, 3] * 3
[1, 2, 3] / 2
[1, 2, 3][2]

For each of the examples below, write what the program would print to the command line.

Sample Program Output
my_list = []
for i in range(5):
    my_list += [i]

print(my_list)
my_list = ["a", 2, "3"]
list_accumulator = []
for i in my_list:
    list_accumulator += [i]

print(list_accumulator)
my_list = ["a", 7.2, 0]
list_list_list = my_list * 3
string_accumulator = ""
for i in range(1, len(list_list_list) + 1):
    string_accumulator += str(list_list_list[-i])

print(string_accumulator)