🧱 Python OOP – Master Object-Oriented Programming in Python | TechTown.in

Object-Oriented Programming (OOP) is one of the most powerful programming paradigms, and Python supports it beautifully.

If you’re aiming to organize your code better, build scalable applications, or simply think like a real software engineer — learning OOP in Python is a must.

In this guide, you’ll explore classes, objects, inheritance, encapsulation, and more, all with simple code examples and real-world use cases.


🔰 What is OOP?

OOP is a programming approach that uses “objects” to represent real-world entities. These objects bundle data (attributes) and functions (methods) together inside classes.

✅ Key Benefits:

  • Code reusability
  • Better structure and modularity
  • Easy to maintain and debug

🧱 Python Class and Object Basics

🧩 Create a Class

class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age

    def greet(self):
        print(f"Hello, my name is {self.name}")

🔹 Create an Object

p1 = Person("Tanmay", 22)
p1.greet()  # Output: Hello, my name is Tanmay

🧠 Understanding __init__() Method

  • It’s a special method (constructor)
  • Automatically called when you create an object
  • Used to initialize variables
def __init__(self, name):
    self.name = name

🧰 Object Methods

Methods are functions defined inside a class:

def greet(self):
    print("Hi, I’m", self.name)

🕵️ Encapsulation: Data Protection

Encapsulation hides internal object details:

class Student:
    def __init__(self):
        self.__marks = 0  # private variable

    def set_marks(self, m):
        self.__marks = m

    def get_marks(self):
        return self.__marks

✅ Use __ to make attributes private.


🧬 Inheritance: Reuse Code Across Classes

class Animal:
    def speak(self):
        print("Animal speaks")

class Dog(Animal):
    def bark(self):
        print("Dog barks")

d = Dog()
d.speak()  # From Animal
d.bark()   # From Dog

🎯 Reuse properties and methods from another class.


🔁 Polymorphism: One Interface, Many Forms

class Car:
    def fuel(self):
        print("Petrol")

class ElectricCar(Car):
    def fuel(self):
        print("Electricity")

vehicle = ElectricCar()
vehicle.fuel()  # Output: Electricity

✅ Same method name behaves differently in different classes.


📦 Class vs Object

TermMeaning
ClassBlueprint or template
ObjectReal-world instance of a class

🧪 Real-World Example – Bank Account

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

    def deposit(self, amount):
        self.balance += amount

    def display(self):
        print(f"{self.owner} → ₹{self.balance}")

📝 Summary – OOP Pillars in Python

ConceptDescription
ClassTemplate for creating objects
ObjectInstance of a class
InheritanceOne class inherits from another
EncapsulationHide internal variables and methods
PolymorphismSame method, different behavior

🏁 Final Thoughts

OOP in Python helps you organize your code in a logical and efficient way, just like how real systems and software work in the real world.

From game development to enterprise systems and AI models, Object-Oriented Programming is everywhere.

Start small, practice each concept, and you’ll soon be designing powerful, modular applications with confidence.


📘 Keep mastering Python with modern tutorials at TechTown.in