👋 Work With Me

I help startups and teams build production-ready apps with Django, Flask, and FastAPI.

Let’s Talk →

I'm always excited to take on new projects and collaborate with innovative minds.

Address

No 7 Street E, Federal Low-cost Housing Estate, Kuje, Abuja 903101, Federal Capital Territory

Social Links

Tutorials

Python Tutorial – Lesson 15: Object-Oriented Programming (OOP) in Python

OOP lets us organize code into classes and objects, making it more reusable, readable, and easier to maintain.

Python Tutorial – Lesson 15: Object-Oriented Programming (OOP) in Python

So far, we’ve worked with variables, functions, and data structures. But as projects grow, managing code becomes harder. That’s where Object-Oriented Programming (OOP) comes in.

OOP lets us organize code into classes and objects, making it more reusable, readable, and easier to maintain.


✅ What is OOP?

  • Class → A blueprint (like a template) for creating objects.
  • Object → An instance of a class (like an actual product built from the blueprint).
  • Method → Functions defined inside a class.
  • Attributes → Variables that belong to an object.

Think of a class as a blueprint for a car, and each car built from it is an object. 🚗


🔹 Creating a Class & Object

 
# Defining a class
class Car:
   def __init__(self, brand, model):
       self.brand = brand
       self.model = model
       
   def drive(self):
       print(f"{self.brand} {self.model} is driving!")
       
# Creating objects
car1 = Car("Toyota", "Corolla")
car2 = Car("Tesla", "Model S")
# Using objects
car1.drive()
car2.drive()

Output:

 
Toyota Corolla is driving!
Tesla Model S is driving!

🔹 The __init__ Method

The __init__ method is a constructor. It runs automatically when an object is created.
Here, we used it to set brand and model.


🔹 Class Attributes vs Instance Attributes

  • Instance Attributes → Unique to each object.
  • Class Attributes → Shared across all objects.
 
class Dog:
   species = "Mammal"   # Class attribute
   
   def __init__(self, name, age):
       self.name = name   # Instance attribute
       self.age = age
       
dog1 = Dog("Buddy", 3)
dog2 = Dog("Lucy", 5)
print(dog1.species)   # Mammal
print(dog2.species)   # Mammal
print(dog1.name)      # Buddy

🔹 Inheritance (Reusing Classes)

OOP allows us to create new classes from existing ones.

 
class Animal:
   def __init__(self, name):
       self.name = name
       
   def speak(self):
       print(f"{self.name} makes a sound.")
       
# Child class inheriting from Animal
class Cat(Animal):
   def speak(self):
       print(f"{self.name} says Meow!")
       
# Using inheritance
cat1 = Cat("Whiskers")
cat1.speak()

Output:

 
Whiskers says Meow! 

🔹 Encapsulation (Hiding Details)

Encapsulation restricts direct access to some data. In Python, we use _ (protected) and __ (private) to signal restricted access.

 
class BankAccount:
   def __init__(self, balance):
       self.__balance = balance   # Private attribute
       
   def deposit(self, amount):
       self.__balance += amount
       
   def get_balance(self):
       return self.__balance
       
account = BankAccount(100)
account.deposit(50)
print(account.get_balance())   # 150

🔹 Polymorphism (One Interface, Many Forms)

Polymorphism allows different classes to share the same method name but behave differently.

 
class Bird:
   def speak(self):
       print("Chirp Chirp!")
       
class Dog:
   def speak(self):
       print("Woof Woof!")
       
# Same method name, different behavior
for animal in (Bird(), Dog()):
   animal.speak()

Output:

 
Chirp Chirp!
Woof Woof!

📝 Quick Recap

  • Class & Object → Blueprint and instance.
  • Attributes → Data stored in objects.
  • Methods → Functions inside a class.
  • Inheritance → Reusing code from parent classes.
  • Encapsulation → Protecting data.
  • Polymorphism → Same method name, different behavior.

🎯 Practice Exercises

  1. Create a Student class with attributes name and age. Add a method greet() that prints:
    "Hello, my name is <name> and I am <age> years old."
  2. Create a Shape class with a method area().
    • Inherit Rectangle and Circle classes.
    • Override area() to calculate correctly for each shape.
  3. Create a BankAccount class with:
    • Methods for deposit, withdraw, and checking balance.
    • Prevent withdrawing more than the balance.

 

Up Next Lesson 16: Advanced OOP Concepts (Inheritance, Encapsulation & Polymorphism in Depth)

💼 Need a Developer?

I'm Kingsley Odume, a Django, Flask, and FastAPI developer with experience building SaaS platforms, APIs, and modern web apps. If you're a recruiter or business owner looking for a reliable software developer, let's connect!

🚀 Hire Me

Python, Python tutorials, Python Beginners series, Python Step-byStep
4 min read
Aug 20, 2025
By Kingsley Odume
Share

Leave a comment

Your email address will not be published. Required fields are marked *

Related posts

Aug 29, 2025 • 3 min read
Step-by-Step: Building a SaaS App with Django + Stripe Payments

Learn how to build a real-world SaaS app with Django and Stripe paymen...

Aug 21, 2025 • 3 min read
Final Wrap-Up: Python Tutorial Series for Beginners

Congratulations on completing the Python Tutorial Series! 🚀 In this w...

Aug 20, 2025 • 4 min read
Final Lesson: Mini Project – Putting It All Together in Python

In this final lesson, we’ll bring all those concepts together in a Min...

Your experience on this site will be improved by allowing cookies. Cookie Policy