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 is a final recap quiz! Enjoy!

1 2 3 4 5 6 7

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, 4], [5, 6]}
x[0].add(1)
print(x[0])

Error
Explanation:

This results in a TypeError: unhashable type: 'list'. The error already occurred in the very first line: you cannot have a mutable element inside a set!

Question 2

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

1
2
x = {3, 4}
print(x[0])

Error
Explanation:

Remember that sets do not have an ordering! So you cannot access the first element of a set, because there is no such thing.

Question 3

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

1
2
3
4
features = [1.1, 2.2]
labels = [1, 0]
data = [(label, feature) for feature, label in zip(features, labels)]
print(data[0])

(1,1.1)
Explanation:

I used list comprehension in Line 3. This builds a list of tuples, using zip() to 'zip up' features and labels. Note I swapped label and feature in the list comprehension just to trip you up! I could have just written it as data = list(zip(labels, features)).

Question 4

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

1
2
3
names = ["Joe", "Harry", "William", "Luca"]
ids = {name: i for (i, name) in enumerate(names)}
print(ids["William"])

2
Explanation:

I used dictionary comprehension in Line 2 (in combination with enumerate). enumerate() generates an index for each item in names, and I used this to generate values for a dictionary.

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
class Letter:
    def __init__(self, content=""):
        self.content = content 

class Person:
    def __init__(self, name):
        self.name = name

    def read(self, letter):
        print(letter.content.upper())

letter = Letter("Darling")     
person = Person("Grandma")
person.read(letter)

DARLING
Explanation:

Here, we have an example of how a Person takes in a Letter instance as an argument to its .read() method. Inside the method, it will print out the content of the letter (in CAPITALS).

Question 6

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
21
22
23
24
25
26
class Song:
    def __init__(self, title):
        self.title = title

    def __str__(self):
        return self.title

class Playlist:
    def __init__(self, name):
        self.name = name
        self.songs = []

    def add_song(self, song):
        self.songs.append(song)

    def play(self, index=0):
        try:
            print(self.songs[index].title)
        except KeyError:
            pass

playlist = Playlist("The 90's")
playlist.add_song(Song("I Want It That Way"))
playlist.add_song(Song("Macarena"))
playlist.add_song(Song("My Heart Will Go On"))
playlist.play(1)

Macarena
Explanation:

Here, you create a Playlist instance, and added three new Song instances to playlist. playlist.play(1) then picks the song at index 1 to play, which prints out "Macarena".

Question 7

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 House:
    def __init__(self, num_of_rooms=2):
        self.rooms = [Room(i+1) for i in range(num_of_rooms)]

    def build(self):
        for room in self.rooms:
            print(room.number)

class Room:
    def __init__(self, number):
        self.number = number


house = House(5)
house.build()

1 2 3 4 5
Explanation:

Here is an example of how you can initialise objects as attributes from within a class constructor itself, rather than passing objects in from outside the constructor as in the previous quiz question. You can consider this as "composition" in a strictest sense.