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
- 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.
- 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.
- 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 ElectricCar that inherits from the Car class.
Create a Python class called StudentRecord with attributes name, age, and grades.
Create a Python class called Course with attributes name, teacher, and students.
Create a Python class called Shape with a method to calculate the area of the shape.
Create a Python class called Employee with attributes name and salary.
Create a Python class called Manager that inherits from the Employee class.