Python OOP


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.

What Is Object Oriented Programming?

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.

Why Python Uses OOP

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.

Key Concepts in Python OOP

Before writing code, you need to understand the four basic ideas behind OOP.

Classes

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.

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

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

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)

Understanding the __init__() Method

The __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.

Real Life Example of Python OOP

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.

Benefits of Using OOP in Python

OOP offers many advantages:

Code Reuse

Once you create a class, you can reuse it for hundreds of objects without rewriting anything.

Better Organization

You can split large programs into small logical units. Each class handles one part of the logic.

Easier Maintenance

If something changes, you fix the logic inside the class once, and all objects benefit.

Real-World Modeling

You can design your program the same way real systems work. This makes complex applications easier to build.

How Python Implements OOP

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.

Essential Tips for Using 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.

Practical Examples

  1. Creating a Simple Class

class User:
    def __init__(self, username):
        self.username = username
  1. Adding a Method

def greet(self):
    print("Welcome", self.username)
  1. Creating an Object

u1 = User("Anita")
u1.greet()
  1. Multiple Objects

u2 = User("Meera")
u2.greet()
  1. Changing Attribute Values

u1.username = "Anita Sharma"
  1. Object With Multiple Attributes

class Book:
    def __init__(self, title, price):
        self.title = title
        self.price = price
  1. Adding Methods in Book Class

def info(self):
    print(self.title, "-", self.price)
  1. List of Objects

books = [Book("Python Basics", 300), Book("Math Guide", 200)]
  1. Looping Through Objects

for b in books:
    b.info()
  1. Updating Object Data

books[0].price = 350

Summary of the Tutorial

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.


Practice Questions

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.


Try a Short Quiz.

coding learning websites codepractice

No quizzes available.

Go Back Top