Python

 

Introduction

Python is an object-oriented programming (OOP) language, meaning it allows us to define and manipulate classes and objects. Classes serve as blueprints for creating objects, while objects are instances of those classes. Understanding how to create and use Python classes and objects is essential for writing efficient and reusable code.

Objective

In this tutorial, we will cover how to:

  • Create a class in Python
  • Instantiate objects from a class
  • Access and modify object attributes
  • Define methods within a class

Python Code Example

class Car:
    def __init__(self, make, model, year):
        self.make = make
        self.model = model
        self.year = year

    def description(self):
        return f"{self.year} {self.make} {self.model}"

    def start_engine(self):
        return f"The {self.make} {self.model}'s engine is now running."

# Creating an object (instance) of the class 'Car'
my_car = Car("Toyota", "Corolla", 2020)

# Accessing attributes of the object
print(my_car.description())

# Calling a method from the object
print(my_car.start_engine())

Program Explanation

The program above demonstrates how to create a class, instantiate objects, and define methods. Here’s a breakdown of the program structure:

  1. Class Definition: The class Car is defined with an __init__ method, which is the constructor. It initializes the attributes make, model, and year for each object.
  2. Instance Creation: An object my_car is created by calling the class Car with arguments for make, model, and year.
  3. Method Definition: The description and start_engine methods are defined within the class to operate on the object’s data.
  4. Object Usage: The object my_car is used to call the methods and access its attributes. We print out the description and call the start_engine method to simulate starting the car.

How to Run the Program

To run this program:

  1. Save the code in a file with a .py extension, for example, car_program.py.
  2. Open a terminal or command prompt.
  3. Navigate to the folder where the Python file is saved.
  4. Run the program by typing python car_program.py (or python3 car_program.py depending on your setup).
© 2025 Learn Programming. All rights reserved.

 

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

Your email address will not be published. Required fields are marked *

error

Enjoy this blog? Please spread the word :)