This is an archived version of the course. Please find the latest version of the course on the main webpage.

Chapter 1: Introduction

Quiz

face Josiah Wang

As usual, let’s warm up with a quiz to make sure you still remember everything we have covered!

1 2 3 4 5 6 7 8 9 10 11

Question 1

What is the value of y after executing the following piece of code? Type Error if you are expecting an error.

x = [9, 0, 2, 1, 3]
print(x[-3])

2
Explanation:

The negative index will return the third element from the end of the list (or equivalently x[2] in this piece of code).

Question 2

What is the value of y after executing the following piece of code? Type Error if you are expecting an error.

x = [9, 0, 2, 1, 3]
print(x[1::2])

[0, 1]
Explanation:

The list slicing operation starts from x[1] to the end of the list, and increments by 2 at each step. This skips one element each time, resulting in only x[1] and x[3] left in the resulting list.

Question 3

What is the value of y after executing the following piece of code? Type Error if you are expecting an error.

x = 2
y = x < 5 and x not in [3, 4, 7, 8]

True
Explanation:

The first expression x < 5 evaluates to True. The second expression x not in [3, 4, 7, 8] also evaluates to True, since 2 is not a member of the list. Therefore, y is True since both expressions are True.

Question 4

What is the value of y after executing the following piece of code? Type Error if you are expecting an error. You can refer to the official documentation for the statistics module if needed.

import statistics
y = statistics.mode([1, 2, 2, 3, 5, 5, 5, 6])
5
Explanation:

This question is merely to make you aware of the statistics module. The mode() function returns the most frequent element in the list.

Question 5

What is output of the following piece of code? Type Error if you are expecting an error.

point = (3, 5)
point[1] += 1
print(point[1])

Error
Explanation:

Remember that tuples are immutable! So you cannot modify the value of a tuple element directly.

Question 6

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

numbers = [1, 2, 3, 4]
numbers[1] = "two"
print(numbers)

[1, "two", 3, 4]
Explanation:

The elements in a list do not have to be of the same type.

Question 7

What is the value of y after executing the following piece of code? Type Error if you are expecting an error. Remember to include the quotes to a string if the output is a string.

x = 3
y = "2"
y += str(x)

"23"
Explanation:

This is a string concatenation operation. x in converted to the str "3", which is then concatenated to "2", resulting in the string "23".

Question 8

What is the output of the following code snippet? Type Error if you are expecting an error. Type Infinite if you are expecting an infinite loop.

1
2
3
4
5
6
7
8
counter = 1
number = 1
while counter < 5:
    if counter % 3 == 0:
        continue
    number += counter
    counter += 1
print(number)
Infinite
Explanation:

While the author of this code probably expects the program to add 1, 2, and 4 (skipping 3), this ends up in an infinite loop because the value of counter will be stuck at 3 (Lines 4 and 5) forever, as there is no chance of the code ever reaching Line 7 (counter += 1) once counter reaches 3!

Question 9

What is the value of y after executing the following piece of code? Type Error if you are expecting an error. Type Infinite if you are expecting an infinite loop.

words = ["welcome", "to", "imperial", "college"]
for word in words:
    for char in word:
        if char in "aeiou":
            continue
        print(char, end="")
    print()

wlcm t mprl cllg
Explanation:

This piece of code will iterate over each word in the list, and print out each of the character in the word if it is not a vowel.

Question 10

What is the value of y after executing the following piece of code? Type Error if you are expecting an error. Type Infinite if you are expecting an infinite loop.

for counter in range(0, 5, 2):
    print(counter+1)

1 3 5
Explanation:

The loop iterates over the range 0 to 5 (exclusive) with an increment of 2. So it will iterate over 0, 2, and 4, and print out the values 1, 3, and 5.

Question 11

What is the value of y after executing the following piece of code? Type Error if you are expecting an error.

x = 0
y = x in range(5)

True
Explanation:

Like lists, the range() object can also be used with the in operator to check for membership in a range of numbers. Note that in here is a sequence membership operator, and is different from the in operator when used with a for loop.