Skip to content
IT Vizag
← Objects & Classes

Why objects?

Lesson 1 of 10 · 2 min read

Bundle data and behaviour together, as real things do.

🎬 Video version coming soon — read it now

So far you've kept data in dictionaries and written separate functions to work on it. That works, but as programs grow, the data and the functions that use it drift apart. Object-oriented programming (OOP) keeps them together.

Things have data and actions

Think of a mobile phone:

  • Data (attributes): brand, battery level, contacts
  • Actions (methods): call, charge, take a photo

A bank account has a holder, a balance and a list of transactions, and it can deposit, withdraw and print a statement. In OOP you describe that once, in a class, and then create as many accounts (objects) as you need.

Class vs object

  • A class is a blueprint: the plan for a house.
  • An object (or instance) is one thing built from it: an actual house in Seethammadhara, another in Gajuwaka.

You've been using objects all along

name = "vizag"
print(name.upper())       # a method of the str class

marks = [78, 92]
marks.append(65)          # a method of the list class

print(type(name), type(marks))

Every string is an object of class str, every list an object of class list. Now you'll write your own classes.

Dictionary vs class

# Dictionary + separate function
account = {"holder": "Ravi", "balance": 5000}
def deposit(acc, amount):
    acc["balance"] += amount

# Class: data and behaviour together (next lesson explains every line)
class Account:
    def __init__(self, holder, balance=0):
        self.holder = holder
        self.balance = balance

    def deposit(self, amount):
        self.balance += amount

ravi = Account("Ravi", 5000)
ravi.deposit(1000)
print(ravi.balance)   # 6000

With the class, ravi.deposit(1000) reads naturally, and every account is guaranteed to have a holder and a balance.

When to use classes

Use one when you have several things of the same kind, each with its own data, plus behaviour that belongs to them. Don't force everything into classes: a small script with a few functions is often better. Python lets you choose.

Tip: Frameworks you'll use later, such as FastAPI, Django and PyTorch, are built from classes. Understanding OOP makes their code readable.

In one line

A class is a blueprint that bundles data and actions; objects are the individual things built from it.

Saved on this device. Sign in to keep progress everywhere.

Share with a friend