Complete the code to define the method that checks if two objects are equal.
class Box: def __eq__(self, other): return self.size [1] other.size
The __eq__ method should return True if the sizes are equal, so we use the == operator.
Complete the code to define the method that checks if one object is less than another.
class Box: def __lt__(self, other): return self.weight [1] other.weight
The __lt__ method means 'less than', so we use the < operator.
Fix the error in the method that checks if one object is greater than or equal to another.
class Box: def __ge__(self, other): return self.height [1] other.height
The correct operator for 'greater than or equal to' is >=. The symbol => is invalid in Python.
Fill both blanks to create a dictionary comprehension that maps words to their lengths only if the length is greater than 3.
words = ['apple', 'bat', 'carrot', 'dog'] lengths = {word: [1] for word in words if len(word) [2] 3}
The dictionary maps each word to its length, so the value is len(word). The condition keeps words longer than 3, so the operator is >.
Fill all three blanks to create a dictionary comprehension that maps uppercase keys to values only if the value is positive.
data = {'a': 1, 'b': -2, 'c': 3}
result = { [1]: [2] for k, v in data.items() if v [3] 0 }The keys are converted to uppercase with k.upper(). The values are v. The condition keeps values greater than 0, so the operator is >.