Course Progress53%
⏳ 8 min read
Classes and Objects
The cornerstone of Object-Oriented Programming — bundling data and behaviour together into reusable blueprints.
"A class is a blueprint. An object is something built from that blueprint. Every car in a factory is a different object, but they all share the same design."
— ShurAIThe Problem Classes Solve
Imagine storing data about three students using plain variables:
python — messy without classes
student1_name = "Riya"
student1_score = 92
student1_grade = "A"
student2_name = "Arjun"
student2_score = 75
student2_grade = "B"
# Ten students? Forty variables. Chaos.
A class bundles the data and behaviour of a thing into one reusable unit:
python — clean with a class
class Student:
pass # empty class for now
# Create two objects from the same blueprint
s1 = Student()
s2 = Student()
# Give each object its own data
s1.name = "Riya"
s1.score = 92
s2.name = "Arjun"
s2.score = 75
print(s1.name, s1.score) # Riya 92
print(s2.name, s2.score) # Arjun 75
Class vs Object — The Blueprint Analogy
🛠️
Class = Blueprint
Defines the structure: what data and actions this type of thing has.
Exists once in your code.
Example:
Exists once in your code.
Example:
class Car:
🚗
Object = Instance
A specific thing built from the blueprint.
Can create thousands.
Example:
Can create thousands.
Example:
my_car = Car()
A Proper Class with Data and Methods
python
class Dog:
def __init__(self, name, breed):
self.name = name # store data on the object
self.breed = breed
def bark(self):
print(f"{self.name} says: Woof!")
def describe(self):
print(f"{self.name} is a {self.breed}")
# Create two Dog objects
dog1 = Dog("Bruno", "Labrador")
dog2 = Dog("Luna", "Husky")
dog1.bark() # Bruno says: Woof!
dog2.describe() # Luna is a Husky
print(dog1.name) # Bruno
Key Terms
| Term | Meaning | Example |
|---|---|---|
| class | The blueprint definition | class Dog: |
| object / instance | A specific thing built from the class | dog1 = Dog(...) |
| attribute | Data stored on an object | dog1.name |
| method | A function that belongs to a class | dog1.bark() |
Real Example — BankAccount Class
python
class BankAccount:
def __init__(self, owner, balance=0):
self.owner = owner
self.balance = balance
def deposit(self, amount):
self.balance += amount
print(f"Deposited {amount}. Balance: {self.balance}")
def withdraw(self, amount):
if amount > self.balance:
print("Insufficient funds!")
else:
self.balance -= amount
print(f"Withdrew {amount}. Balance: {self.balance}")
acc = BankAccount("Riya", 1000)
acc.deposit(500)
acc.withdraw(200)
acc.withdraw(2000)
output
Deposited 500. Balance: 1500
Withdrew 200. Balance: 1300
Insufficient funds!
"OOP isn't about writing more code — it's about writing better-organised code. When a class models a real-world concept cleanly, the rest of the program almost writes itself."
— ShurAI🧠 Quiz — Q1
What is a class in Python?
🧠 Quiz — Q2
What is the difference between a class and an object?
🧠 Quiz — Q3
How do you access the name attribute on an object called dog1?
🧠 Quiz — Q4
What is a method?