Tuesday, October 27, 2026 New tutorials published every day
Home
Tutorials
Core PHP PHP Course JavaScript React & Next.js Python WordPress Artificial Intelligence
Free Tools
JSON Formatter Base64 Encoder / Decoder Password Generator Text to QR Code Generator Word Counter Case Converter Age Calculator Color Converter Number to Words All Free Tools
Projects
PHP Projects — all projects
Resources
Search tutorials About CodingCush Contact
More
Login
Python

Python Classes and Objects Explained with Simple Examples

Python classes and objects explained simply: blueprints vs instances, __init__, self, class vs instance attributes, with runnable Student and bank examples.

In Python, a class is a blueprint and an object is a thing built from that blueprint. You define a class once with the class keyword — describing what data it holds and what actions it can do — and then you create as many objects (also called instances) as you need from it. For example, a Student class is the blueprint; rahul and priya, each with their own name and marks, are two objects made from it. That is the whole idea of object-oriented programming (OOP) in Python, and this guide builds it up with small, runnable examples.

If variables and lists already feel comfortable, classes are your next level-up. They are how real Python projects organise code once "a pile of functions" stops being fun to maintain — usually around the same week your script passes 300 lines and starts arguing back.

Why Do We Need Classes at All?

Imagine tracking students with separate lists: one list for names, one for marks, one for courses. Keeping those lists in sync is painful, and one wrong insert quietly corrupts everything. A class solves this by bundling the data and the behaviour together: each student object carries its own name and marks, plus ready-made actions like "print my report card". One object, one student, no sync headaches.

# Without classes: parallel lists that must stay in sync
names = ["Rahul", "Priya"]
marks = [82, 91]

# With a class: each object keeps its own data together
class Student:
    pass

rahul = Student()
rahul.name = "Rahul"
rahul.marks = 82
print(rahul.name, rahul.marks)   # Rahul 82

The second style is still clumsy — we are gluing attributes on from outside. The proper way is to let the class set itself up, which is exactly what the next section does. If the words "list" and "dictionary" still blur together for you, five minutes with our Python list vs dictionary guide will make the examples below much easier.

Python class and object example — CodingCush in-article image
A class is the blueprint; each object built from it holds its own data.

Your First Class: __init__ and self

Two names appear in almost every Python class, and both frighten beginners more than they deserve. __init__ is a special method that runs automatically when an object is created — use it to store the starting data. self simply means "this particular object", so self.name reads as "this student's name". The official tutorial introduces classes the same gentle way in the Python documentation on classes, which is worth bookmarking once this guide clicks.

class Student:
    def __init__(self, name, marks):
        self.name = name      # this object's name
        self.marks = marks    # this object's marks

    def report(self):
        print(f"{self.name} scored {self.marks} marks")

rahul = Student("Rahul", 82)   # __init__ runs here
priya = Student("Priya", 91)

rahul.report()   # Rahul scored 82 marks
priya.report()   # Priya scored 91 marks

Notice what happened: one blueprint, two independent objects. Changing priya.marks would never touch Rahul's data, because each object owns its attributes. Methods are just functions written inside the class, and they always receive self as the first parameter — Python passes it automatically, so you never write it when calling rahul.report().

Class Attributes vs Instance Attributes

An instance attribute belongs to one object (self.name — every student has a different one). A class attribute belongs to the class itself and is shared by all objects — perfect for values that are the same for everyone, like the school name or a pass mark. Mixing them up is a classic beginner bug, so here is the difference in one table and one example.

  Instance attribute Class attribute
Where defined Inside __init__, using self. Directly in the class body
Belongs to One object The class — shared by all objects
Example name, marks school, pass_marks
Change it and… Only that object changes Every object sees the new value
class Student:
    school = "Coding Cush Academy"   # class attribute (shared)
    pass_marks = 40                  # class attribute (shared)

    def __init__(self, name, marks):
        self.name = name             # instance attribute
        self.marks = marks           # instance attribute

    def passed(self):
        return self.marks >= Student.pass_marks

print(Student("Aman", 35).passed())   # False
print(Student("Sara", 77).passed())   # True

A Real-World Example: A Tiny Bank Account

Classes earn their keep when behaviour has rules. A bank account must never let a withdrawal exceed the balance — with a class, that rule lives in exactly one place, and no code outside can accidentally break it by editing a random variable.

class BankAccount:
    def __init__(self, owner, balance=0):
        self.owner = owner
        self.balance = balance

    def deposit(self, amount):
        self.balance += amount
        print(f"Deposited {amount}. Balance: {self.balance}")

    def withdraw(self, amount):
        if amount > self.balance:
            print("Not enough balance!")
        else:
            self.balance -= amount
            print(f"Withdrew {amount}. Balance: {self.balance}")

acct = BankAccount("Lav", 500)
acct.deposit(200)    # Balance: 700
acct.withdraw(1000)  # Not enough balance!
acct.withdraw(300)   # Balance: 400

This bundling of data plus the rules that protect it is called encapsulation, and it is the first of the four OOP ideas interviewers love. The others — inheritance (a SavingsAccount class reusing BankAccount), polymorphism and abstraction — all build on the class-and-object foundation you now have. Python even offers a shortcut for data-heavy classes: the dataclasses module in the Python docs writes the boring __init__ code for you once you are comfortable doing it by hand.

Classes also pair beautifully with files: each object can save its own state, a pattern you will recognise from our tutorial on reading and writing files in Python.

Three Mistakes Almost Every Beginner Makes

Forgetting self. If you define a method as def report(): without self, Python will still pass the object in — and complain that it received an argument the method does not accept. The fix is always the same: add self as the first parameter of every normal method. Creating attributes in random places. You can glue a new attribute onto an object from anywhere, like we did in the very first example, but a class becomes hard to read when attributes appear in five different methods. A good habit: create every attribute in __init__, even if its starting value is just 0, an empty list or None. Then anyone reading the class sees the full shape of the object in one place. Confusing the class with the object. Writing Student.marks asks the blueprint for marks — but marks live on individual students. You almost always want rahul.marks, one specific object's data. When an error mentions a missing attribute, this mix-up is the first suspect to check, before blaming Python, the universe, or your keyboard.

One more healthy habit: name classes with a capital letter (Student, BankAccount) and objects in lowercase (rahul, acct). Python does not force this, but every Python programmer reads that capital letter as "this is a blueprint" instantly. Conventions like this are free communication — take them.

Frequently Asked Questions

What is the difference between a class and an object in Python?

A class is the blueprint that defines attributes and methods. An object is an actual instance created from that blueprint, with its own data. One class can create unlimited objects.

What does self mean in Python?

self refers to the current object. Inside a method, self.name means "this object's name". Python passes it automatically as the first argument of every method.

What is __init__ in a Python class?

__init__ is the constructor method. It runs automatically when you create an object and is used to set up the object's starting attributes, like a student's name and marks.

Do I need classes for small Python scripts?

Honestly, no. Short scripts are fine with functions alone. Classes start paying off when your program models real things with data and rules — students, accounts, products — or when the same structure repeats many times.

Conclusion

A class is a blueprint, an object is what you build from it, __init__ sets each object up, and self points at the object you are working with. With just those four ideas you can model students, bank accounts, products — almost anything a real project needs. Write the Student example by hand once, change the names, break it on purpose, and fix it. That ten-minute experiment teaches more than re-reading this page three times, enjoyable as that would be for us.

Lavkush
Written by

Lavkush Super Admin

Lavkush Kumar is a software developer with over 6 years of experience turning ideas into websites and web applications. He holds a Master of Computer Applications (MCA) degree and enjoys building for the web, exploring new tools, and sharing what he learns along the way.

Through CodingCush, Lavkush creates easy-to-follow tutorials and practical guides to help students and beginners learn software development without feeling overwhelmed. His approach is simple: learn by doing, build real projects, and keep improving one step at a time.

Whether you’re just starting out or looking to sharpen your skills, CodingCush is all about making learning practical, beginner-friendly, and fun.

0 Comments

Leave a Comment