Skip to main content
  1. Notes/
  2. Python 🐍/

πŸ—οΈ Object-Oriented Programming (OOP) Concepts in Python | Lesson 15

Python Progmramming Course
Table of Contents
Python - This article is part of a series.
Part 15: This Article

πŸ—οΈ Object-Oriented Programming (OOP) Concepts in Python
#

OOP is a powerful programming style that helps you organize your code by bundling data and functionality into objects. It’s used to build complex programs that are easier to manage, reuse, and expand. Let’s learn the key OOP concepts in Python step-by-step! πŸš€


What is OOP? πŸ€”
#

OOP uses objects that contain both data (attributes) and behaviors (methods). Think of objects as real-world entities like a car, student, or bank account.


1. Classes and Objects πŸ›οΈ
#

  • Class: Blueprint or template to create objects.
  • Object: A specific instance of a class.
class Dog:
    def __init__(self, name, age):   # Constructor method
        self.name = name
        self.age = age

    def bark(self):
        print(f"{self.name} says Woof!")

# Creating objects
my_dog = Dog("Buddy", 3)
my_dog.bark()  # Buddy says Woof!
  • self refers to the object itself.
  • __init__ initializes object attributes.

2. Attributes and Methods 🌟
#

  • Attributes: Variables that belong to the object, like name and age.
  • Methods: Functions that belong to the object, like bark().

3. Inheritance πŸ“¦
#

Create a new class based on an existing class to reuse or extend functionality.

class Animal:
    def eat(self):
        print("Eating...")

class Cat(Animal):   # Cat inherits Animal
    def meow(self):
        print("Meow!")

kitty = Cat()
kitty.eat()    # Eating...
kitty.meow()   # Meow!

4. Encapsulation πŸ”’
#

Control access to attributes/methods using private variables (prefix with _ or __).

class Person:
    def __init__(self, name, age):
        self.name = name
        self.__age = age  # Private attribute

    def get_age(self):
        return self.__age

person = Person("Asha", 30)
print(person.get_age())  # 30
# print(person.__age)    # Error! Cannot access directly

5. Polymorphism πŸ”„
#

Using the same method name in different classes with different behaviors.

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

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

def make_sound(animal):
    animal.sound()

dog = Dog()
cat = Cat()

make_sound(dog)  # Woof!
make_sound(cat)  # Meow!

Practice Questions with Solutions πŸ†
#

  1. Create a class Car with attributes make and year. Add a method to display these details.
  2. Create a class Rectangle with methods to calculate area and perimeter.
  3. Demonstrate inheritance by creating a class ElectricCar that inherits from Car and adds a battery attribute.

Step-by-Step Mini Project: Bank Account πŸ’°
#

Build a simple Bank Account class with deposit, withdraw, and balance checking.

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

    def deposit(self, amount):
        self.balance += amount
        print(f"Deposited {amount}. New balance: {self.balance}")

    def withdraw(self, amount):
        if amount <= self.balance:
            self.balance -= amount
            print(f"Withdrew {amount}. New balance: {self.balance}")
        else:
            print("Insufficient funds!")

# Usage
account = BankAccount("Rohit", 1000)
account.deposit(500)
account.withdraw(2000)

Checklist for This Chapter βœ…
#

  • Defined and used classes and objects
  • Created attributes and methods
  • Applied inheritance to reuse code
  • Practiced encapsulation for data hiding
  • Understood polymorphism with method overriding

You now have the tools to design well-structured, real-world programs with Python using OOP! Practice by modeling everyday objects and systems! 🎯

Aryan
Author
Aryan
A little bit about you
Python - This article is part of a series.
Part 15: This Article

Related

⏰ Working with Date and Time in Python | Lesson 16
Python Progmramming Course
🎬 Strings in Python | Lesson 04
Python Progmramming Course
πŸ‘Ά Python Basics | Lesson 02
Python Progmramming Course
πŸ“„ File Handling in Python | Lesson 14
Python Progmramming Course
πŸ“‹ Lists in Python | Lesson 05
Python Progmramming Course
πŸ“– Dictionaries in Python | Lesson 07
Python Progmramming Course