Category: python
Python OOPS
Published on 24 Jul 2026
Explanation
Object-Oriented Programming (OOP) is a programming paradigm that organizes software around objects rather than functions. An object represents a real-world
Code:
# Simple Python Program
class Student:
pass
student1 = Student()
student2 = Student()
print(type(student1))
print(type(student2))
Explanation
A class is a blueprint used to create objects. It defines the properties (attributes) and behaviors (methods) that objects created
Code:
class Student:
def display(self):
print('Welcome to Python OOP')
student1 = Student()
student2 = Student()
student1.display()
student2.display()
Explanation
A constructor is a special method that is automatically executed whenever an object is created. In Python, the constructor is
Code:
class Employee:
def __init__(self, emp_id, name, salary):
self.emp_id = emp_id
self.name = name
self.salary = salary
def display(self):
print('ID:', self.emp_id)
print('Name:', self.name)
print('Salary:', self.salary)
employee = Employee(101, 'John', 50000)
employee.display()
Explanation
Instance variables are variables that belong to individual objects. They are created using the self keyword inside the constructor. Each
Code:
class Product:
def __init__(self, product_id, name, price):
self.product_id = product_id
self.name = name
self.price = price
def display(self):
print('Product ID:', self.product_id)
print('Name:', self.name)
print('Price:', self.price)
product1 = Product(1, 'Laptop', 65000)
product2 = Product(2, 'Mouse', 750)
product1.display()
print('----------------')
product2.display()
Explanation
Object-oriented programming is widely used to model real-world entities such as students, employees, customers, bank accounts, products, and vehicles. Each
Code:
class BankAccount:
def __init__(self, account_number, customer_name, balance):
self.account_number = account_number
self.customer_name = customer_name
self.balance = balance
def deposit(self, amount):
self.balance += amount
def withdraw(self, amount):
if amount <= self.balance:
self.balance -= amount
else:
print('Insufficient Balance')
def display(self):
print('Account Number:', self.account_number)
print('Customer:', self.customer_name)
print('Balance:', self.balance)
account = BankAccount(1001, 'Praveen', 10000)
account.deposit(2000)
account.withdraw(1500)
account.display()