CBSE 2026 results are out, Mukul scored a perfect 100/100 in Computer ScienceSee all toppers →

KwickAcademy Python · 8 min · free

Object Oriented Python: Classes, Objects and __init__

8 min5 KwickClipsFull text belowFree
Kajal Ma'am (MCA), teaching since 2004Remembered in this browser

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?

PartWhat it isExample
Attributedata it hasaman.marks
Methodaction it doesaman.passed()
Bracketsonly for methodspassed()

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__

MethodUsed byWritten for
__str__print(), str()users
__repr__lists, shellprogrammers
Only __repr__print uses itboth

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.

The 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

CourseUnit
CBSE Class 12 Computer Science (083)Computational Thinking and Programming – 2
Programming All levels PythonObject-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.

Want a plan that actually fits your board dates?

Ask Kajal Ma'am directly, 20+ years teaching computer science. Free demo class first, no payment.

Talk to Kajal Ma'am on WhatsApp

Or see the Class 12 Computer Science course →

Studying outside India?

We coach CBSE, IGCSE & international students across the globe, one-to-one, in your local time zone.

Visit International →