KwickAcademy Python · 8 min · free
Object Oriented Python: Classes, Objects and __init__
Learn classes and objects in Python: write a class, set data with __init__ and self, add methods, use __str__ and __repr__, and build a bank account.
On screen in this lesson
Class and object
| Class: a blueprint, like an identity card design |
| Object: one real thing made from the blueprint |
| One class can make many objects |
| Each object keeps its own data |
What is self?
| self means this object, the one being used |
| It is always the first parameter of a method |
| Python passes it for you; you never type it in the call |
| Forget self and the call gives TypeError |
Attribute or method?
| Part | What it is | Example |
|---|---|---|
| Attribute | data it has | aman.marks |
| Method | action it does | aman.passed() |
| Brackets | only for methods | passed() |
Printing an object
| print(aman) shows a strange text by default |
| Something like <__main__.Student object at 0x...> |
| __str__ gives a friendly text for users |
| __repr__ gives a clear text for programmers |
__str__ vs __repr__
| Method | Used by | Written for |
|---|---|---|
| __str__ | print(), str() | users |
| __repr__ | lists, shell | programmers |
| Only __repr__ | print uses it | both |
Quick recap
| A class is a blueprint; an object is one instance |
| __init__ sets up each new object; self means this object |
| Attributes hold data; methods are actions |
| __str__ for users, __repr__ for programmers |
| A method can protect data, like a bank balance |
Quick answers
What is self in Python?
self means this object, the one the method is working with; it is the first parameter of every method.
What is the difference between __str__ and __repr__?
__str__ is used by print and is for users; __repr__ is used inside lists and the shell and is for programmers.
KwickClips from this lesson
Short clips, one idea each. Good for revision the night before.
What is a class?41 sec
What is self?40 sec
Why does aman.marks have no brackets?37 sec
What is __str__ used for?39 sec
How does withdraw stop overdrawing?37 secThe full lesson, in text
Hello students, welcome to Kwickprep. A school has hundreds of students, and every student has a name, a roll number and marks. Do we need hundreds of separate variables? No. Today we will learn classes and objects, the init method, self, and we will build a small bank account.
First, two new words. A class is a blueprint, which means a plan for making things, like the design of a school identity card. An object is one real thing made from that plan, like Riya's own identity card. One class can make as many objects as we need. And each object keeps its own data, so Riya's card and Aman's card show different names.
We write the word class, then the class name, then a colon. By habit, class names start with a capital letter. The word pass means do nothing for now, because a class body cannot be left empty. Writing Student with round brackets creates a new object, also called an instance. So riya and aman are two instances of Student. Type of riya, dot name with two underscores on each side, gives the class name, Student. The is operator asks, are these the very same object? The answer is False, because they are two different objects.
Now let us give each object its own data. A function written inside a class is called a method. The method named init, with two underscores on each side, runs by itself whenever a new object is created. Its job is to set up the object. When we write Student of Riya and eighty eight, Python creates the object and calls init. Inside, self dot name equals name stores Riya inside this object. We read the data back with a dot, so riya dot name gives Riya.
Students often ask, what exactly is self? Self means this object, the one we are working with right now. It is always written as the first parameter of every method. A parameter is a name that receives a value when the method is called. Python fills in self for you, so Student of Riya and eighty eight passes only two values. If you forget to write self in the method, Python gives a type error when you call it.
Here n is the name and m is the marks. The third line stores n in self dot name and m in self dot marks, both in one line. Self dot name and self dot marks are instance attributes. An instance attribute is data that belongs to one object. The passed method is an action the object can do. The greater than or equal to sign checks if marks are thirty three or more. Aman has thirty, so passed gives False. Pause and predict. What prints if Aman has exactly thirty three? True, because greater than or equal to includes thirty three.
Let us compare the two. An attribute is data the object has, like marks, and we read it with a dot. A method is an action the object does, like checking a result. Round brackets come only after a method, because we are calling it to run. Board exams often ask you to spot this difference in output questions.
What happens if we print an object directly? By default, Python shows a strange looking text. It shows the class name and a memory address, which changes on every run. To fix this, we can write the str method, with two underscores on each side. It returns friendly text for users. We can also write the repr method, which returns clear text for programmers.
Here is the str method. It must return a string, which is text. When we print the neha object, Python quietly calls str for us. So instead of a memory address, it prints Student colon Neha. The str function, written str of neha, also uses this method.
Now the repr method. Its text should look like the code that makes the object. The f before the quotes makes an f-string, which puts the value of self dot name inside the text. When an object sits inside a list, printing the list uses repr, not str. So we see Student, with Neha in quotes, inside square brackets. If a class has only repr, print uses repr as a backup.
Here is the difference in one table. The str method is used by print and by the str function, and it is written for users. The repr method is used inside lists and in the Python shell, and it is written for programmers. If you write only repr, print uses it too, so repr is a safe first choice.
Now a small project, a bank account. Here b is the starting balance, and init is written on one line to save space. The attribute bal stores the balance. The deposit method takes an amount. Plus equals adds the amount to what is already stored. We open an account with five hundred rupees and deposit two hundred. So the balance becomes seven hundred.
A bank must never let you take out more money than you have. The withdraw method checks, is the amount less than or equal to the balance? Only then, minus equals takes the amount away. Then the method returns the balance. We ask for nine hundred from an account with seven hundred. The check is False, so nothing is taken, and it prints seven hundred. Pause and predict. What prints if we withdraw exactly seven hundred? Zero, because seven hundred is equal to the balance.
Let us revise what we learned today. A class is a blueprint, and an object is one instance made from it. The init method sets up every new object, and self means this object. Attributes hold data, and methods are actions the object can do. The str method gives text for users, and repr gives text for programmers. And a method can protect data, like refusing to withdraw more than the balance. Try adding a str method to the bank account yourself.
Courses that teach this
| Course | Unit |
|---|---|
| CBSE Class 12 Computer Science (083) | Computational Thinking and Programming – 2 |
| Programming All levels Python | Object-Oriented Programming |
Voice-over in this lesson is AI-generated. The script is written and checked by Kajal Ma'am. Boards can revise a syllabus mid-year, so confirm anything you plan around against the official board circular. Keep your passwords, OTPs and ID numbers to yourself — we never ask for them. To reach Kajal Ma'am, use the WhatsApp button; sharing your number there is how we call you back.
Free to watch, no sign-up. Live classes with Kajal Ma'am are the paid course; these lessons stay free either way.

