
Object-Oriented Programming (OOP) in Python
Object-Oriented Programming (OOP) is a way of writing computer programs using the concept of "objects." An object is a combination of data (attributes) and actions (methods) that can perform tasks or represent something in the real world.
Classes: A class is like a blueprint or template for creating
objects. It defines the attributes (characteristics) and methods (actions)
that objects of that class will have.
Objects: An object is an instance of a class. It is a specific,
tangible thing created from the class blueprint, with its own set of data
and behavior.
Attributes: Attributes are pieces of data that describe the state of
an object. For example, a Car class may have attributes such as color,
brand, and speed.
Methods: Methods are functions that are defined within a class and
can perform actions or operations on the object's data. For example, a Car
class may have methods such as accelerate() and brake().
def __init__(self, name, age):
self.name = name
self.age = age
def bark(self):
print(f"{self.name} says woof!")
In this example, we define a Dog class with attributes name and age, and a
method bark().
We use the __init__() method (constructor) to initialize the object's
attributes when it is created.
The bark() method defines the behavior of the Dog object.
my_dog = Dog("Buddy", 3)
# Access attributes
print("Name:", my_dog.name)
print("Age:", my_dog.age)
# Call methods
my_dog.bark() # Output: Buddy says woof!
We create an instance of the Dog class called my_dog, passing values for
name and age.
We access the object's attributes (name and age) using dot notation.
We call the bark() method on the my_dog object to make it bark.
Modularity: OOP allows us to break down complex problems into
smaller, more manageable pieces (objects and classes).
Reuseability: Once a class is defined, it can be reused to create
multiple objects with similar characteristics and behavior.
Encapsulation: Objects encapsulate their data and behavior, hiding
the internal implementation details from the outside world.
1. Bank Account Management System:
Q. Develop a program to manage bank accounts. Each account should have
attributes like account number, account holder name, balance, etc., and
methods to deposit, withdraw, and check balance.
def __init__(self, account_number, account_holder, balance=0):
self.account_number = account_number
self.account_holder = account_holder
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!")
def check_balance(self):
print(f"Current balance: ${self.balance}")
# Example Usage
account1 = BankAccount(123456, "Alice", 1000)
account1.deposit(500)
account1.withdraw(200)
account1.check_balance()
2. Student Management System
Q. Create a program to manage student records. Each student should have
attributes like student ID, name, age, grade, etc., and methods to update
information and display details.
def __init__(self, student_id, name, age, grade):
self.student_id = student_id
self.name = name
self.age = age
self.grade = grade
def update_grade(self, new_grade):
self.grade = new_grade
print(f"Grade updated to {new_grade}")
def display_details(self):
print(f"Student ID: {self.student_id}")
print(f"Name: {self.name}")
print(f"Age: {self.age}")
print(f"Grade: {self.grade}")
# Example Usage
student1 = Student(101, "Alice", 15, "A")
student1.display_details()
student1.update_grade("B")
3. Car Rental System
Q. Implement a car rental system with attributes like car make, model, year,
availability, etc., and methods to rent and return cars.
def __init__(self, make, model, year, available=True):
self.make = make
self.model = model
self.year = year
self.available = available
def rent(self):
if self.available:
self.available = False
print("Car rented successfully!")
else:
print("Car not available for rent")
def return_car(self):
if not self.available:
self.available = True
print("Car returned successfully!")
else:
print("Car already available")
# Example Usage
car1 = Car("Toyota", "Camry", 2020)
car1.rent()
car1.return_car()
4. Library Management System
Q. Create a program to manage library resources such as books. Each book
should have attributes like title, author, ISBN, availability, etc., and
methods to borrow and return books.
def __init__(self, title, author, isbn, available=True):
self.title = title
self.author = author
self.isbn = isbn
self.available = available
def borrow(self):
if self.available:
self.available = False
print("Book borrowed successfully!")
else:
print("Book not available for borrowing")
def return_book(self):
if not self.available:
self.available = True
print("Book returned successfully!")
else:
print("Book already available")
# Example Usage
book1 = Book("Python Programming", "John Smith", "123456789")
book1.borrow()
book1.return_book()
Summary:
In summary, Object-Oriented Programming (OOP) is a powerful programming paradigm that allows us to model real-world entities as objects with attributes and methods. By organizing code into classes and objects, we can write more organized, reusable, and maintainable code. OOP is widely used in software development for building applications of all sizes and complexities.