6. Object-Oriented Programming (OOP)
Classes & Objects (OOP Explained Simply)
Understand Object-Oriented Programming without confusing jargon: learn how a class works like a blueprint, what __init__ and self actually mean, and when to use objects.
- Understand classes as blueprints and objects as independent instances
- Initialize starting attributes with __init__ and track state using self
- Bundle data and behavior together into clean object methods
Earlier we used dictionaries to represent things like a student:
{"name": "Alex", "lessons_done": 3}
That works nicely—until you want each student to also carry actions (like "complete a lesson" or "print a progress badge"). Keeping data in one place and separate helper functions somewhere else can get messy in bigger apps.
A class lets you bundle data and actions together in one neat blueprint!
Think "Blueprint vs Real Thing"
class Learner (Blueprint)
name, track, lessons_done
alex = Learner("Alex", "Python")
lessons_done = 2
maya = Learner("Maya", "SQL")
lessons_done = 1
Each object created from `Learner` keeps its own separate `name` and `lessons_done` values via `self`.
- A
classis an empty house blueprint drawn on paper. You cannot live inside a blueprint. - An
objectis a real house built from that blueprint. You can build 100 houses from 1 blueprint, and painting House #1 blue does not paint House #2 blue!
Writing Your First Class Slowly
class Learner:
def __init__(self, name, track):
self.name = name
self.track = track
self.lessons_done = 0
def finish_lesson(self):
self.lessons_done = self.lessons_done + 1
print(f"{self.name} finished lesson #{self.lessons_done} in {self.track}!")Let's demystify the two strange-looking words here:
__init__(double underscores on both sides): This special function runs automatically the moment you create a newLearner(...). Its job is to set the starting values (name,track, andlessons_done = 0).self:selfsimply means "THIS specific object." When you callalex.finish_lesson(), Python passesalexasself, soalex.lessons_donegoes up without touchingmaya.lessons_done!
Building & Using Objects
alex = Learner("Alex", "Python")
maya = Learner("Maya", "SQL")
alex.finish_lesson() # Alex finished lesson #1 in Python!
alex.finish_lesson() # Alex finished lesson #2 in Python!
maya.finish_lesson() # Maya finished lesson #1 in SQL!
print(alex.lessons_done) # 2
print(maya.lessons_done) # 1Look how clean and readable alex.finish_lesson() is—just like a built-in method such as my_list.append(...)! (In fact, lists and strings in Python are built using classes too!)
Every function inside a class needs self as its very first parameter (def finish_lesson(self):). If you forget self, calling alex.finish_lesson() will crash with TypeError.
Try It Yourself in the Playground
Try adding a method add_bonus(self, count) or creating a third learner:
Click “Run Code” to execute this snippet right in your browser.
Mini-Challenge: Add a show_balance(self) method to BankAccount that prints "{owner}'s current balance is ${balance}" and call it on both accounts.
Quick Checkpoints
When does Python run __init__ automatically?
In def deposit(self, amount):, what does self represent?