-
Hajipur, Bihar, 844101
Object Oriented Programming (OOP) is one of the most powerful features in Python. It helps you structure your code in a cleaner and more reusable way. Instead of writing long procedural scripts, OOP lets you build programs using objects that represent real-world things like students, cars, accounts, or products.
In this tutorial, you’ll learn how Python handles OOP, what classes and objects are, why OOP makes coding easier, and how to use it effectively in your projects.
OOP is a method of structuring programs around objects. An object groups data and behaviour together. This makes programs easier to scale and understand.
For example:
A Student object can store name, roll number, and marks
A Car object can store brand, model, and color
Each of these objects can also have actions, such as drive, brake, or display details
Python supports OOP fully, and its syntax is simple enough for beginners to learn quickly.
Python uses OOP because it solves common problems that appear as programs grow:
You can avoid repeating code
You can create reusable templates
Large projects become more organized
You can model real-world systems easily
Different parts of the program can be managed separately
OOP brings structure and clarity, which is important for real applications like web development, automation systems, desktop apps, and data models.
Before writing code, you need to understand the four basic ideas behind OOP.
A class is like a blueprint. It defines what an object will contain and how it will behave. Think of it as a design template.
Example:
class Student:
pass
This class doesn’t do anything yet, but it acts as a starting point for creating Student objects.
An object is created from a class. It represents one physical or logical item.
Example:
s1 = Student()
s2 = Student()
Here, s1 and s2 are two different objects created from the same template.
Attributes store data inside an object. These can be things like name, age, price, or quantity.
Example:
class Student:
def __init__(self, name, grade):
self.name = name
self.grade = grade
Now every Student object will have its own name and grade.
Methods are functions inside a class. They perform actions related to the object.
Example:
class Student:
def __init__(self, name):
self.name = name
def intro(self):
print("My name is", self.name)
__init__() MethodThe __init__() method runs automatically when an object is created. It helps you set default values for each object.
Example:
class Car:
def __init__(self, brand, model):
self.brand = brand
self.model = model
Now if you create two cars:
c1 = Car("Toyota", "Fortuner")
c2 = Car("Honda", "City")
Each car holds its own data.
Let’s build a simple program that models a Bank Account.
class Account:
def __init__(self, holder, balance):
self.holder = holder
self.balance = balance
def deposit(self, amount):
self.balance += amount
def info(self):
print("Account Holder:", self.holder)
print("Balance:", self.balance)
Creating objects:
a1 = Account("Riya", 5000)
a1.deposit(2000)
a1.info()
This program behaves like a real bank account system. Each account is an object with its own balance and holder name.
OOP offers many advantages:
Once you create a class, you can reuse it for hundreds of objects without rewriting anything.
You can split large programs into small logical units. Each class handles one part of the logic.
If something changes, you fix the logic inside the class once, and all objects benefit.
You can design your program the same way real systems work. This makes complex applications easier to build.
Python treats everything as an object, including numbers, strings, and functions. When you create a class, Python internally builds a new data type that your program can use.
Even this works:
print(type(10))
print(type("Hello"))
Both return a class type because everything in Python is built around OOP.
Here are a few practices that help keep your classes clean and useful:
Keep one class focused on one purpose
Use clear and simple method names
Use __init__() to set up object data
Break large tasks into small methods
Avoid making extremely long classes
Use comments only when needed
These habits make your code easier for you and others to understand.
Creating a Simple Class
class User:
def __init__(self, username):
self.username = username
Adding a Method
def greet(self):
print("Welcome", self.username)
Creating an Object
u1 = User("Anita")
u1.greet()
Multiple Objects
u2 = User("Meera")
u2.greet()
Changing Attribute Values
u1.username = "Anita Sharma"
Object With Multiple Attributes
class Book:
def __init__(self, title, price):
self.title = title
self.price = price
Adding Methods in Book Class
def info(self):
print(self.title, "-", self.price)
List of Objects
books = [Book("Python Basics", 300), Book("Math Guide", 200)]
Looping Through Objects
for b in books:
b.info()
Updating Object Data
books[0].price = 350
Python OOP helps you build programs using classes and objects. A class acts as a blueprint, while objects hold actual data. Attributes store information, and methods perform actions related to the object. OOP makes programs cleaner, reusable, and easier to maintain, especially when they grow large.
Mastering OOP is essential for real-world Python projects like automation, software development, data modeling, and web applications.
Q1. Write a Python program to create a Book class with attributes title and author.
Q2. Write a Python program to add a method display() in the Book class to show book information.
Q3. Write a Python program to create multiple objects of the Book class and call the display() method for each.
Q4. Write a Python program to create a base class Employee and a derived class Manager that inherits from Employee.
Q5. Write a Python program to use super() in the Manager class to call the parent constructor.
Q6. Write a Python program to add a private variable in a class (using __var) and restrict direct access from outside the class.
Q7. Write a Python program to implement a sound() method in two different classes (e.g., Dog, Cat) and use polymorphism to call them.
Q8. Write a Python program to change the value of an object property after the object has been created.
Q9. Write a Python program to delete a property of an object using del.
Q10. Write a Python program to create a class with both class-level and object-level attributes, and print both.