Complete the code to define a class attribute named count initialized to 0.
class MyClass: [1] = 0
self.count instead of just count.self.def keyword which is for methods, not attributes.The class attribute is defined directly inside the class, without self or def. So count = 0 is correct.
Complete the code to access the class attribute count inside the method show_count.
class MyClass: count = 5 def show_count(self): print([1])
self.count() which tries to call an attribute as a method.count which is undefined in method scope.self.count which looks for instance attribute, not class attribute.To access a class attribute inside a method, use the class name followed by the attribute: MyClass.count.
Fix the error in the code to correctly increment the class attribute count by 1 inside the method increment.
class MyClass: count = 0 def increment(self): MyClass.[1] += 1
self.count which may create an instance attribute instead.Count.increment as attribute.The class attribute count should be accessed with the exact name and class: MyClass.count.
Fill both blanks to create a dictionary comprehension that maps each number to its square, but only for numbers where the class attribute threshold is greater than 5.
class Numbers: threshold = 5 squares = {x: x[1]2 for x in range(1, 6) if x [2] threshold}
* instead of ** for power.> instead of <.threshold correctly.The operator for power is **. The condition should check if x is less than the threshold (5) to include numbers less than 5.
Fill all three blanks to create a dictionary comprehension that maps uppercase keys to values, but only include items where the value is greater than 10.
class Data: items = {'a': 5, 'b': 15, 'c': 20} filtered = [1]: [2] for [3], v in items.items() if v > 10}
items instead of k in the loop.The keys are converted to uppercase with k.upper(), values are v, and the loop variable for keys is k.