Friday, August 22, 2025

🐍 Object-Oriented Programming (OOP) in Python – A Beginner-Friendly Guide

 

Object-Oriented Programming (OOP) is one of the most important concepts in programming. It allows you to organize your code in a way that makes it easier to manage, reuse, and extend.

In this tutorial, we’ll cover OOP step by step with simple examples.


πŸ”Ή 1. What is OOP?

OOP is a programming paradigm based on the concept of objects.

  • An object is something that has attributes (data) and methods (functions).

  • Example: A car has attributes like brand, model, and methods like drive().


πŸ”Ή 2. Creating a Class

A class is like a blueprint for objects.


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

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

animals = [Dog(), Cat()]
for animal in animals:
    print(animal.speak())
Here:
  • __init__ is a constructor → it runs when we create an object.

  • self represents the current object.


πŸ”Ή 3. Creating Objects


car1 = Car("Tesla", "Model S")
car2 = Car("Toyota", "Corolla")


print(car1.drive())
print(car2.drive())

✅ Output:


Tesla Model S is driving...
Toyota Corolla is driving...

πŸ”Ή 4. Adding More Attributes

You can add more attributes to describe the object.


class Car:
    def __init__(self, brand, model, fuel):
        self.brand = brand
        self.model = model
        self.fuel = fuel

    def info(self):
        return f"{self.brand} {self.model} runs on {self.fuel}."

car = Car("Honda", "Civic", "Petrol")
print(car.info())

✅ Output:

Honda Civic runs on Petrol.

πŸ”Ή 5. Inheritance

One of the best features of OOP is inheritance – when one class can use features of another.


class ElectricCar(Car):
    def __init__(self, brand, model, battery):
        super().__init__(brand, model, "Electric")
        self.battery = battery

    def battery_info(self):
        return f"{self.brand} {self.model} has a {self.battery} kWh battery."


ecar = ElectricCar("Tesla", "Model 3", 75)
print(ecar.info())
print(ecar.battery_info())

✅ Output:


Tesla Model 3 runs on Electric.
Tesla Model 3 has a 75 kWh battery.

πŸ”Ή 6. Encapsulation

Encapsulation means restricting direct access to data. We can use _ or __ to make variables private.


class BankAccount:
    def __init__(self, owner, balance):
        self.owner = owner
        self.__balance = balance  # private attribute

    def deposit(self, amount):
        self.__balance += amount
        return f"Balance updated: {self.__balance}"

    def get_balance(self):
        return self.__balance


acct = BankAccount("Alice", 1000)
print(acct.deposit(500))
print(acct.get_balance())

πŸ”Ή 7. Polymorphism

Polymorphism means many forms – different classes can have the same method but with different behavior.


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

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

animals = [Dog(), Cat()]
for animal in animals:
    print(animal.speak())

✅ Output:


Woof!
Meow!

πŸ”Ή 8. Summary

  • Class = blueprint

  • Object = instance of class

  • Attributes = variables inside class

  • Methods = functions inside class

  • Inheritance = reuse classes

  • Encapsulation = hide details

  • Polymorphism = same method, different behavior


Try it Yourself!

You can run the  codes here and practice:


✨ With OOP, you can structure your Python projects in a way that is clean, reusable, and powerful!

No comments:

Post a Comment

🐍What is scikitlearn??