Sep 16, 2024
Class Definition
class ClassName:
class Car:
(note the capitalization)pass
as a placeholder initially if no content is provided.Attributes and Methods
drive(self)
- prints "This car is driving."stop(self)
- prints "This car has stopped."__init__
Methoddef __init__(self, make, model, year, color):
self.make = make
self.model = model
self.year = year
self.color = color
car1 = Car("Chevy", "Corvette", 2021, "blue")
print(car1.make) # Output: Chevy
car1.drive() # Output: This Corvette is driving.
car1.stop() # Output: This Corvette has stopped.
car2 = Car("Ford", "Mustang", 2022, "red")
print(car2.make) # Output: Ford
car2.drive() # Output: This Mustang is driving.
car2.stop() # Output: This Mustang has stopped.