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

Here’s a quick quiz to warm up your brain before we begin!

1 2 3 4 5

Question 1

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

1
2
3
x = ([3], (5, 6))
x[0].append(1)
print(x[0])

[3, 1]
Explanation:

While a tuple is not mutable, the elements inside a tuple might be mutable. In this case, the list x[0] can be modified.

Question 2

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

1
2
3
words = {"a": ["apple", "alpaca"], "b": ["bag", "book"]}
words.setdefault("c", []).append("cow")
print(words["c"])

['cow']
Explanation:

This one might be a bit confusing. The .setdefault() method is a bit like .get(), except that it also initialises the dictionary with a default value for a key if the key does not exist. In this case, if "c" is not a key in the dictionary, then it sets words["c"] = [] and returns a reference to this new empty list. So now you can append to words["c"].

Question 3

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

1
2
members = {"harry", "william", "luca", "joe"}
print({"josiah", "luca"}.issubset(members))

False
Explanation:

The set {"josiah", "luca"} is not a subset of members, since "josiah" is not in members. Boo 😢

Question 4

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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
class Lamp:
    def __init__(self, state=False):
        self.state = state

    def turn_off(self):
        self.state = False

    def turn_on(self):
        self.state = True

    def toggle(self):
        self.state = not self.state

    def is_on(self):
        return self.state

lamp = Lamp()
lamp.turn_off()
lamp.toggle()
print(lamp.is_on())

True
Explanation:

The Lamp instance is initialised to False, since no input argument was given in Line 17. The method call lamp.turn_off() does not change the state (still False), while lamp.toggle() will flip the state to True.

Question 5

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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
class Dog:
    def __init__(self, species, age=1):
        print("Dog")
        self.species = species
        self.age = age
        self.barks = {
            "poodle": "arf",
            "chihuahua": "waff"
        }
        self.bark()

    def bark(self):
        print(self.barks.get(self.species, "woof") * self.age)

dog = Dog("chihuahua", 2)

waffwaff
Explanation:

The constructor sets up the attribute values of the Dog instance, and then calls the .bark() method. In this method, the code in Line 13 will retrieve the correct bark sound based on the species (defaulting to "woof"), and print out the bark sound self.age times.