Classes and Objects in Python — Bringing Code to Life

If you’ve read the first article of this series, you already know what Object-Oriented Programming (OOP) is and how it differs from the traditional procedural approach.
Now, let’s take the next step — understanding the heart of OOP: Classes and Objects.
Think of this as the point where your code stops being just lines of instructions and starts acting like real-world entities.
🧩 What Are Classes and Objects?
Let’s start with a simple question:
How do you describe a “car” in real life?
You might say it has a color, brand, speed, and it can drive or stop.
In OOP, we represent such things as objects — something that has data (attributes) and actions (methods).
But before we can make an object, we need a class — a blueprint that tells Python what an object should look like.
🏗️ Class — The Blueprint
A class is like a recipe or a template.
It defines what data (attributes) and actions (methods) every object of that type will have.
For example:
class Car:
def __init__(self, color, speed):
self.color = color
self.speed = speed
def drive(self):
print(f"Driving the {self.color} car at {self.speed} km/h")
def brake(self):
print(f"The {self.color} car has stopped.")
Here’s what’s happening:
class Car: — defines a blueprint called Car.
__init__() — a special method called the constructor, automatically runs when you create an object.
self — refers to the current object being created.
color and speed — attributes (data).
drive() and brake() — methods (behaviors).
🚗 Object — The Real Instance
Now that we have a blueprint, we can create actual cars!
my_car = Car("Red", 120)
friend_car = Car("Blue", 100)
my_car.drive()
friend_car.brake()
Each object (my_car, friend_car) has its own copy of attributes.
They both come from the same blueprint (Car), but they can have different colors, speeds, and behaviors.
So, in simple words:
Class → Design,
Object → Real thing created using that design.
🎓 A Simpler Example — The Student Class
Let’s represent something more relatable: a student.
class Student:
def __init__(self, name, grade):
self.name = name
self.grade = grade
def display_info(self):
print(f"Student: {self.name}, Grade: {self.grade}")
s1 = Student("Alice", "A")
s2 = Student("Bob", "B")
s1.display_info()
s2.display_info()
Output:
Student: Alice, Grade: A
Student: Bob, Grade: B
Here:
Each student has their own data (name, grade).
The method display_info() defines how that data is displayed.
Both s1 and s2 are independent objects created from the same Student class.
⚙️ Understanding the self Keyword (Improved & Beginner-Friendly)
If there's one part of classes that confuses beginners the most, it’s this little word: self.
But don’t worry — once you understand it, classes instantly make sense.
👉 What is self?
self simply refers to the current object that is being created or used.
Think of it like this:
When you make my_car = Car("Red", 120)
→ self becomes my_car inside the classWhen you make friend_car = Car("Blue", 100)
→ self becomes friend_car
So each object gets its own separate data, and self helps Python know which object we are talking about.
🧠 A Simple Analogy
Imagine a class as a form with blank fields:
Student Form
Name: ___
Grade: ___
Each student fills in their own details.
Inside the system:
When Alice fills the form → self refers to Alice
When Bob fills the form → self refers to Bob
So self.name is Alice’s name when Alice is the object,
and self.name is Bob’s name when Bob is the object.
🔍 Why is self needed?
Because inside a class, Python must know:
Which object's
name?Which object's
grade?Which object's
color?Which object's
speed?
If we didn’t have self, every object would get confused with others.
🧪 Let's Look at a Clear Example
class Example:
def show(self):
print("Hello from", self)
When we do:
obj = Example()
obj.show()
Python actually converts it internally to:
Example.show(obj)
So self is automatically assigned the object obj.
📝 In Short
selfrepresents the object calling the method.It connects the class blueprint to the actual object in memory.
Without
self, your class wouldn’t know which object’s data to access.
🧠 Why Do We Need Classes and Objects?
Without classes, our code becomes messy as we try to manage data and behavior separately.
Using classes gives us several advantages:
Modularity – Each class focuses on a single purpose (like
Car,Student, etc.).Reusability – Once you write a class, you can reuse it in multiple programs.
Scalability – You can easily expand your code by adding more classes or features.
Real-World Mapping – Classes make your code feel natural, as they represent real-world entities.
💡 Analogy: Why This Matters
Imagine a school management system.
You’ll have:
A
Studentclass (name, roll number, grade)A
Teacherclass (subject, experience)A
Courseclass (title, duration)
Each one has unique data and behaviors.
When you create an object, you’re basically saying, “Here’s a real example of that class.”
This modular design makes complex systems much easier to manage.
🏁 Conclusion
In this article, you learned:
What classes and objects are.
How to define a class using the
classkeyword.How to create and use objects.
Why
selfis important.And how classes make your programs more structured and realistic.
Classes and objects are the foundation of OOP.
Now that you know how to create them, you’re ready to move on to the next exciting topic — Encapsulation — where we’ll learn how to protect and control access to data inside classes.
Stay tuned for Article 3: “Encapsulation in Python — Protecting Your Data” 🔒
📝 Practice Questions — Test Your Understanding
Here are some simple yet meaningful exercises to strengthen what you learned in this article.
1️⃣ Create a class called Book
Write a Python class that represents a book with:
title
author
price
Add a method display_info() that prints the book's details.
Then create two book objects and display their information.
2️⃣ Build a Phone class
Your class should have:
brand
model
battery_percentage
Add two methods:
charge() → prints “Charging…”
status() → prints battery percentage
Create an object and call these methods.
3️⃣ Create a Pet class
Attributes:
name
animal_type (ex: dog, cat)
Methods:
sound() → print a generic sound:
“Bark” for dog
“Meow” for cat
Something else for others
Hint: Use if–elif inside the method.
4️⃣ Trace the value of self
Given:
class Demo:
def show(self):
print(self)
d1 = Demo()
d2 = Demo()
d1.show()
d2.show()
❓ What do you think the output represents?
(Explain how Python internally treats self.)
5️⃣ Write a class Movie
Attributes:
name
year
rating
Method:
is_hit() → prints
“Hit movie!” if rating > 8
“Average movie” if rating between 5 and 8
“Flop movie” otherwise
Create three objects and test your method.
6️⃣ Predict the Output
class Test:
def __init__(self, x):
self.x = x
def update(self, value):
self.x = value
t1 = Test(5)
t2 = Test(10)
t1.update(20)
print(t1.x)
print(t2.x)
❓ What will be printed?
(Explain why the two objects behave independently.)
7️⃣ Create your own real-world class
Pick anything from your daily life:
Laptop
Bag
Coffee Machine
Bicycle
Teacher
Game Character
Define at least:
2 attributes
2 methods
and then create two objects of that class.
This exercise helps you think in an object-oriented way.



