Python - 25

Python - 25: Working With Multiple Objects

Classes become powerful when you create many objects and organize them in lists.

✦ Python Editor
▶ Output

👀 Show Example Solution
class Car:

    def __init__(self, brand, speed):

        self.brand = brand
        self.speed = speed

    def show_car(self):

        print("Brand:")
        print(self.brand)

        print("Speed:")
        print(self.speed)

        print("---")

car1 = Car("Tesla", 200)

car2 = Car("BMW", 180)

cars = [car1, car2]

for car in cars:

    car.show_car()