


Khusboo Tayal
Python is an object-oriented programming (OOP) language, which means it allows you to define classes and create objects. Understanding classes and objects is essential for writing clean, reusable, and organized code.
In this guide, you’ll learn what classes and objects are in Python, how to define them, and how to use them in real-world scenarios.
You can think of a class as a template, and an object as a specific item created using that template.
You define a class using the class keyword.
class MyClass:
x = 10
This creates a class named MyClass with a variable x.
Once a class is defined, you can create objects from it:
obj = MyClass() print(obj.x) # Output: 10
The __init__() method is called automatically when a class is instantiated. It is used to initialize object properties.
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
p1 = Person("Alice", 30)
print(p1.name) # Output: Alice
print(p1.age) # Output: 30
self refers to the current instance of the class.
You can define functions (methods) inside a class to define behaviors.
class Person:
def __init__(self, name):
self.name = name
def greet(self):
print("Hello, my name is", self.name)
p1 = Person("Bob")
p1.greet() # Output: Hello, my name is Bob
You can modify object properties just like regular variables:
p1.age = 25 print(p1.age) # Output: 25
You can delete object properties using del:
del p1.age
You can also delete the entire object:
del p1
class Student:
school = "ABC School" # Class variable
def __init__(self, name):
self.name = name # Instance variable
s1 = Student("Tom")
s2 = Student("Jerry")
print(s1.school) # Output: ABC School
print(s2.name) # Output: Jerry
If you don't want to add any content to a class, use the pass statement to avoid an error.
class EmptyClass:
passclass BankAccount:
def __init__(self, holder, balance):
self.holder = holder
self.balance = balance
def deposit(self, amount):
self.balance += amount
def display(self):
print(f"Account Holder: {self.holder}, Balance: ₹{self.balance}")
account1 = BankAccount("Amit", 10000)
account1.deposit(5000)
account1.display()
# Output: Account Holder: Amit, Balance: ₹15000
Understanding classes and objects is the foundation of writing scalable and organized code in Python. By leveraging the power of object-oriented programming, you can build efficient and reusable applications with ease.
Start small by creating simple classes, then move on to more complex designs using inheritance, polymorphism, and encapsulation.
Q1: What is the difference between a class and an object?
A class is a blueprint; an object is an instance created from that blueprint.
Q2: Is self a keyword in Python?
No, self is not a keyword but a naming convention used to refer to the instance of the class.
Q3: Can a class contain other classes?
Yes, Python allows nested classes, but it's not common practice unless necessary.
Subscribe to our newsletter to get the latest Python tutorials, project ideas, and updates right to your inbox.
#python #pythonclasses #pythonobjects #objectorientedprogramming #oopinpython #learnpython #pythontutorial #pythonforbeginners #pythoncode #pythonbasics