Lesson 9 No Object is an Island Chapter 1: Introduction [1.1] Recap [1.2] Quiz [1.3] Introduction to Lesson 9 Chapter 2: Object interaction Chapter 3: Refactor your robot! Chapter 4: More iterable objects Chapter 5: List comprehension Chapter 6: JSON files Chapter 7: More classes for your robot! Chapter 8: Python packages Chapter 9: Exception handling Chapter 10: Summary 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 3x = ([3], (5, 6)) x[0].append(1) print(x[0]) Output [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. Check answer! Question 2 What is the output after executing the following piece of code? Type Error if you are expecting an error. 1 2 3words = {"a": ["apple", "alpaca"], "b": ["bag", "book"]} words.setdefault("c", []).append("cow") print(words["c"]) Output ['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"]. Check answer! Question 3 What is the output after executing the following piece of code? Type Error if you are expecting an error. 1 2members = {"harry", "william", "luca", "joe"} print({"josiah", "luca"}.issubset(members)) Output False Explanation: The set {"josiah", "luca"} is not a subset of members, since "josiah" is not in members. Boo 😢 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 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20class 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()) Output 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. 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 Dog: def __init__(self, species, age=1): 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) Output 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. Check answer!