Lesson 10 I am Your Father Chapter 1: Introduction [1.1] Recap [1.2] Quiz [1.3] Introduction to Lesson 10 Chapter 2: Inheritance Chapter 3: A variety of robots! Chapter 4: More functions Chapter 5: Lambda functions Chapter 6: Higher order functions Chapter 7: More file handling Chapter 8: Unit testing Chapter 9: User-defined exceptions Chapter 10: Summary 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 3x = {[3, 4], [5, 6]} x[0].add(1) print(x[0]) Output 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! Check answer! Question 2 What is the output after executing the following piece of code? Type Error if you are expecting an error. 1 2x = {3, 4} print(x[0]) Output 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. Check answer! Question 3 What is the output after executing the following piece of code? Type Error if you are expecting an error. 1 2 3 4features = [1.1, 2.2] labels = [1, 0] data = [(label, feature) for feature, label in zip(features, labels)] print(data[0]) Output (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)). Check answer! Question 4 What is the output after executing the following piece of code? Type Error if you are expecting an error. 1 2 3names = ["Joe", "Harry", "William", "Luca"] ids = {name: i for (i, name) in enumerate(names)} print(ids["William"]) Output 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. Check answer! 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 14class 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) Output 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). Check answer! 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 26class 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) Output 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". Check answer! 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 15class 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() Output 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. Check answer!