Home › Python with Chai › Lesson 19
Classes: the chai shop
Python with Chai, lesson 19 of 20. A short Hinglish video, simple English notes and code you can run, free.
Open the interactive lesson with Run button
The idea
Two shops with separate variables (dukaan1_naam, dukaan2_cups …) get messy fast. A class is the blueprint of one shop.
__init__ sets up each new shop, and self means "this shop": self.naam, self.cups.
A function inside the class is a method. Each object (raju, pappu) keeps its own count.
The code
class ChaiDukaan:
def __init__(self, naam):
self.naam = naam
self.cups = 0
def bechna(self):
self.cups += 1
print(self.naam, self.cups)
raju = ChaiDukaan("Raju")
pappu = ChaiDukaan("Pappu")
raju.bechna()
raju.bechna()
pappu.bechna()
Output:
Raju 1 Raju 2 Pappu 1
Try it yourself
- Add a third shop for "Sita" and sell 3 cups there.
- Add a method hisaab(self) that prints how many cups the shop has sold.
Remember
Class = the blueprint, object = a real shop.
← datetime: shop timing · Finale: you know Python! →
All lessons: Python with Chai