Chapter 2: Lists

Quiz

face Josiah Wang

Quick quiz! There are some teaching materials in here, so don’t skip it!

1 2 3 4 5 6 7

Question 1

What is the output of the following code snippet? Type Error if it results in an error.

numbers = [11, 12, 13, 14, 15, 16]
print(numbers[1])

12
Explanation:

Remember that list indices start at 0, so numbers[1] is the second element in the list.

Question 2

What is the output of the following code snippet? Type Error if it results in an error.

numbers = [11, 12, 13, 14, 15, 16]
print(numbers[3])

14
Explanation:

The index 3 refers to the 4th element in the list, so it's 14.

Question 3

What is the output of the following code snippet? Type Error if it results in an error.

numbers = [11, 12, 13, 14, 15, 16]
print(numbers[6])

Error
Explanation:

There are six items in the list, so the last index is 5. Python will give you an error if you try to go beyond 5. Try it out in an interactive prompt yourself to see what kind of error Python gives you!

Question 4

Write a function call to count the number of elements in a list called numbers? Hint: you have used this function quite a bit so far!

numbers = [11, 12, 13, 14, 15, 16]
len(numbers)
Explanation:

You can get the length/size of the list with the len() function.

Question 5

What is the output of the following code snippet? Type Error if it results in an error.

numbers = [11, 12, 13, 14, 15, 16]
print(numbers[-2])

15
Explanation:

This refers to the second item from the end of the list.

Question 6

What is the output of the following code snippet? Type Error if it results in an error.

numbers = [11, 12, 13, 14, 15, 16]
print(numbers[-7])

Error
Explanation:

Again, you will get an IndexError: list index out of range.

Question 7

What is the output of the following code snippet? Type Error if it results in an error.

numbers = [11, 12, 13, 14, 15, 16]
print(numbers[-6] == numbers[0])

True
Explanation:

Both numbers[0] and numbers[-6] have the same value. In fact, in this case they both point to the exact same object!