Coding Now – Best AI & Full Stack Courses in Delhi NCR | 100% Placement
Limited Offer: Get 50% OFF on AI & Full Stack Courses
📞 Call Now: +91 9667708830
Back to Python Notes
Topic #11

OOPs in Python

What Is Object-Oriented Programming?

Object-Oriented Programming (OOP) structures code around objects — bundles of data (attributes) and behavior (methods) — instead of just functions and logic. Python is a fully object-oriented language.

Classes and Objects

A class is a blueprint; an object is an actual instance created from that blueprint.

class Student:
    def __init__(self, name, course):
        self.name = name
        self.course = course

    def introduce(self):
        print(f"Hi, I'm {self.name}, studying {self.course}")

s1 = Student("Aman", "Python")
s1.introduce()   # Hi, I'm Aman, studying Python

__init__ is the constructor — it runs automatically when a new object is created. self refers to the current object instance.

The Four Pillars of OOP

1. Encapsulation

Bundling data and methods together, and restricting direct access to some of an object's details:

class Account:
    def __init__(self, balance):
        self.__balance = balance   # double underscore → "private"

    def get_balance(self):
        return self.__balance

acc = Account(5000)
print(acc.get_balance())   # 5000
# print(acc.__balance)     # ❌ AttributeError — not directly accessible

2. Inheritance

A class can inherit attributes and methods from another class, promoting code reuse:

class Person:
    def __init__(self, name):
        self.name = name
    def greet(self):
        print("Hello, my name is", self.name)

class Student(Person):        # Student inherits from Person
    def __init__(self, name, course):
        super().__init__(name)
        self.course = course

s = Student("Riya", "AI Engineering")
s.greet()   # Hello, my name is Riya (inherited method)

3. Polymorphism

Different classes can define the same method name, but each behaves differently:

class Dog:
    def speak(self):
        print("Woof!")

class Cat:
    def speak(self):
        print("Meow!")

for animal in [Dog(), Cat()]:
    animal.speak()   # Woof! then Meow!

4. Abstraction

Hiding complex implementation details and exposing only what's necessary, often using abstract base classes:

from abc import ABC, abstractmethod

class Shape(ABC):
    @abstractmethod
    def area(self):
        pass

class Circle(Shape):
    def __init__(self, radius):
        self.radius = radius
    def area(self):
        return 3.14 * self.radius ** 2

c = Circle(5)
print(c.area())   # 78.5

Class Attributes vs Instance Attributes

class Student:
    institute = "CodingNow"   # class attribute — shared by all instances

    def __init__(self, name):
        self.name = name        # instance attribute — unique per object

s1 = Student("Aman")
s2 = Student("Riya")
print(s1.institute, s2.institute)   # CodingNow CodingNow

Key Takeaways

  • A class is a blueprint; an object is a specific instance built from it
  • OOP rests on four pillars: Encapsulation, Inheritance, Polymorphism, and Abstraction
  • Use super() to call the parent class's methods from a child class

Want to go beyond the notes?

Join CodingNow's Python course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available
💬 Talk to Advisor
1
WhatsApp

Latest from Our Blog

Insights on AI, Data Science, Full Stack & Career

View All Articles →