33. Problem to create an OOPS example in Python

In this Python oops program, we will create an OOPS example in Python. Python class called Car with attributes make, model, and year.

OOPS example in Python

Steps to solve the program
  1. The Car class has a constructor method __init__ that takes three parameters: make, model, and year. It initializes the instance variables self.make, self.model, and self.year with the provided values.
  2. The print_details method prints the details of the car, including the make, model, and year. It retrieves the values of the instance variables and prints them with descriptive labels.
  3. The code creates an object car of the Car class with the make “Mahindra”, model “Thar”, and year 2021.
    The print_details method is called on the car object, which prints the details of the car.

				
					class Car:
    def __init__(self, make, model, year):
        self.make = make
        self.model = model
        self.year = year
    
    def print_details(self):
        print("Make:", self.make)
        print("Model:", self.model)
        print("Year:", self.year)

# Create an object of the Car class
car = Car("Mahindra", "Thar", 2021)
car.print_details()

				
			

Output:

				
					Make: Mahindra
Model: Thar
Year: 2021
				
			

Related Articles

Create a Python class called CheckingAccount that inherits from the BankAccount class.

Create a Python class called ElectricCar that inherits from the Car class.

Leave a Comment