Complete the code to create an object of class Person.
class Person: def __init__(self, name): self.name = name p = [1]("Alice") print(p.name)
The correct way to create an object is by calling the class name like a function.
Complete the code to define a destructor method that prints a message when an object is deleted.
class Car: def __del__(self): print([1]) c = Car() del c
The destructor method __del__ should print a string message, so it must be in quotes.
Fix the error in the code to properly initialize the object with a name attribute.
class Animal: def __init__(self, name): [1].name = name pet = Animal("Buddy") print(pet.name)
Inside methods, 'self' refers to the current object and is used to set attributes.
Fill both blanks to create a dictionary comprehension that maps each word to its length only if the length is greater than 3.
words = ["apple", "cat", "banana", "dog"] lengths = {word: [1] for word in words if len(word) [2] 3}
The dictionary comprehension uses len(word) to get length and filters words with length greater than 3 using '>'.
Fill all three blanks to create a dictionary comprehension that maps uppercase keys to their values only if the value is greater than 0.
data = {'a': 1, 'b': 0, 'c': 3}
result = { [1]: [2] for k, v in data.items() if v [3] 0 }The comprehension uses k.upper() as keys, v as values, and filters values greater than 0.