Test your knowledge of Python classes and objects with these fill-in-the-blank exercises. Learn key concepts like attributes, methods, and the __init__ method with answers provided for self-assessment.
Difficulty Levels: Beginner β
Topic: Classes and Objects
class Car:
def __init__(self, make, model):
self.make = make
self.model = model
my_car = Car("Toyota", "Corolla")
The `__init__` method is used to initialize the attributes of the `Car` class.
class Student:
school = "High School"
student1 = Student()
student2 = Student()
student1.school = "Middle School"
Changing the `school` attribute of `student1` will also change it for `student2`.
class Dog:
def bark(self):
print("Woof!")
my_dog = Dog()
my_dog.bark()
The `bark` method is an instance method of the `Dog` class.
class Circle:
pi = 3.14
print(Circle.pi)
The `pi` attribute is a class attribute of the `Circle` class.