DEV Community

M.T.Ramkrushna
M.T.Ramkrushna

Posted on

3. PYTHON ESSENTIALS FOR AI/ML (OOP)

🟦 1. What is OOP?

OOP = a way of organizing code around “objects.”
Objects = things that have properties (data) and behaviors (functions).

Real-life analogy

A Car:

  • Properties → color, speed, brand
  • Behaviors → start(), stop(), accelerate()

In OOP, you'd define a Car class and create many cars (objects/instances) from it.


🟦 2. Class & Object

✔ Class

A class is a blueprint — like a recipe.

✔ Object

An object is the actual thing created from that blueprint.

Example:

class Car:
    pass

my_car = Car()   # object
Enter fullscreen mode Exit fullscreen mode

🟦 3. Attributes (Variables inside objects)

Attributes store data about the object.

Example:

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

my_car = Car("Tesla", "Red")
print(my_car.brand)   # Tesla
print(my_car.color)   # Red
Enter fullscreen mode Exit fullscreen mode

Real-life analogy

__init__() = when you build a car at the factory → you set brand, color, model.


🟦 4. Methods (Functions inside a class)

Methods define behaviors of objects.

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

    def start(self):
        print(self.brand, "is starting...")

tesla = Car("Tesla")
tesla.start()
Enter fullscreen mode Exit fullscreen mode

🟦 5. The self keyword

self refers to the current object.

Real-life analogy:
Think of “my own” — my color, my brand, my speed.


🟦 6. Encapsulation (Hiding data)

We keep some data private to protect it.

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

    def get_balance(self):
        return self.__balance
Enter fullscreen mode Exit fullscreen mode

You cannot directly access __balance from outside.
This prevents accidental misuse — like ATM preventing direct access to internal cash counters.


🟦 7. Inheritance (Child class getting features of parent class)

Just like:

  • Dog 🐶 and Cat 🐱 inherit from Animal

Example:

class Animal:
    def speak(self):
        print("Animal makes a sound")

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

d = Dog()
d.speak()    # Dog barks
Enter fullscreen mode Exit fullscreen mode

Real-life analogy:
A child inherits features from parent → height, hair color, etc.


🟦 8. Polymorphism (Same method name, different behavior)

Example:

class Bird:
    def fly(self):
        print("Bird is flying")

class Airplane:
    def fly(self):
        print("Airplane is flying")

for obj in [Bird(), Airplane()]:
    obj.fly()
Enter fullscreen mode Exit fullscreen mode

Real life:
“drive()” means different things for:

  • car
  • bike
  • truck

🟦 9. Abstraction (Show only important things, hide details)

Real-life example:
When you drive a car, you don’t see the engine or circuits — only steering & pedals.

In code, abstraction hides complexity.

Example using abstract class:

from abc import ABC, abstractmethod

class Payment(ABC):
    @abstractmethod
    def pay(self):
        pass
Enter fullscreen mode Exit fullscreen mode

🟦 10. Full Real-Life Example — ATM System

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

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

    def withdraw(self, amount):
        if amount <= self.__balance:
            self.__balance -= amount
            return "Withdraw successful"
        return "Insufficient funds"

    def get_balance(self):
        return self.__balance


# Creating objects
ali_acc = BankAccount("Ali", 5000)
sara_acc = BankAccount("Sara", 7000)

print(ali_acc.withdraw(2000))
print(sara_acc.deposit(1000))
print(ali_acc.get_balance())
Enter fullscreen mode Exit fullscreen mode

This example uses:
✔ attributes
✔ private variables
✔ methods
✔ constructor
✔ encapsulation


🟦 11. Mini Exercises (Your turn!)

💡 Send me your code and I’ll review it!

1. Create a class Student

With attributes:

  • name
  • age
  • marks

Add method:

  • is_pass() → returns True if marks > 40.

2. Create a class Car

Attributes: brand, speed
Methods: accelerate(), brake()


3. Create two classes

  • Animal (parent)
  • Dog (child) — override a speak() method

Top comments (0)