Employee class

Python Practice › OOP Programs › Program 38

Employee class

EasyOOP Programs · 38 of 48

Create Employee with name and salary and a method to print them.

Output
Asha: Rs 650,000

Approach

  1. Store and print.

Solution

Python
class Employee:    def __init__(self, name, salary):        self.name, self.salary = name, salary     def show(self):        print(f"{self.name}: Rs {self.salary:,}") Employee("Asha", 650000).show()
Output
Asha: Rs 650,000

Learn the concept

New to this topic? Read the lesson, then come back.

More oop programs

← All 48 OOP Programs

Shape with Square and Triangle

Python Practice › OOP Programs › Program 37

Shape with Square and Triangle

EasyOOP Programs · 37 of 48

Create a Shape class with an area method and subclasses Square and Triangle.

Output
Square 16
Triangle 9.0

Approach

  1. Each subclass overrides area().

Solution

Python
class Shape:    def area(self):        raise NotImplementedError class Square(Shape):    def __init__(self, side):        self.side = side    def area(self):        return self.side ** 2 class Triangle(Shape):    def __init__(self, base, height):        self.base, self.height = base, height    def area(self):        return 0.5 * self.base * self.height for shape in [Square(4), Triangle(6, 3)]:    print(type(shape).__name__, shape.area())
Output
Square 16
Triangle 9.0

Learn the concept

New to this topic? Read the lesson, then come back.

More oop programs

← All 48 OOP Programs

Course with students

Python Practice › OOP Programs › Program 36

Course with students

EasyOOP Programs · 36 of 48

Create Course with name, teacher and students, with methods to add and remove students and print the course.

Output
Python by Ms. Rao: Asha, Meena

Approach

  1. Keep students in a list.

Solution

Python
class Course:    def __init__(self, name, teacher):        self.name, self.teacher, self.students = name, teacher, []     def add(self, student):        if student not in self.students:            self.students.append(student)     def remove(self, student):        if student in self.students:            self.students.remove(student)     def show(self):        print(f"{self.name} by {self.teacher}: {', '.join(self.students)}") c = Course("Python", "Ms. Rao")for s in ["Asha", "Ravi", "Meena"]:    c.add(s)c.remove("Ravi")c.show()
Output
Python by Ms. Rao: Asha, Meena

Learn the concept

New to this topic? Read the lesson, then come back.

More oop programs

← All 48 OOP Programs

StudentRecord with average grade

Python Practice › OOP Programs › Program 35

StudentRecord with average grade

EasyOOP Programs · 35 of 48

Create StudentRecord with name, age and grades, with methods for the average and a summary.

Output
Meena, 16, average 86.33

Approach

  1. sum(grades) / len(grades).

Solution

Python
class StudentRecord:    def __init__(self, name, age, grades):        self.name, self.age, self.grades = name, age, grades     def average(self):        return round(sum(self.grades) / len(self.grades), 2)     def show(self):        print(f"{self.name}, {self.age}, average {self.average()}") StudentRecord("Meena", 16, [88, 92, 79]).show()
Output
Meena, 16, average 86.33

Learn the concept

New to this topic? Read the lesson, then come back.

More oop programs

← All 48 OOP Programs

ElectricCar with range

Python Practice › OOP Programs › Program 34

ElectricCar with range

EasyOOP Programs · 34 of 48

Create ElectricCar (inherits Car) with battery_size and range_per_charge and a method to calculate the range.

Output
Tata Nexon EV 300.0 km

Approach

  1. Range here is battery size (kWh) times km per kWh.

Solution

Python
class Car:    def __init__(self, make, model, year):        self.make, self.model, self.year = make, model, year class ElectricCar(Car):    def __init__(self, make, model, year, battery_size, range_per_charge):        super().__init__(make, model, year)        self.battery_size = battery_size            # kWh        self.range_per_charge = range_per_charge    # km per kWh     def total_range(self):        return self.battery_size * self.range_per_charge ev = ElectricCar("Tata", "Nexon EV", 2025, 40, 7.5)print(ev.make, ev.model, ev.total_range(), "km")
Output
Tata Nexon EV 300.0 km

Learn the concept

New to this topic? Read the lesson, then come back.

More oop programs

← All 48 OOP Programs

Car class

Python Practice › OOP Programs › Program 33

Car class

EasyOOP Programs · 33 of 48

Create a Car class with make, model and year and a method to print them.

Output
2024 Mahindra XUV700

Approach

  1. Store and print the attributes.

Solution

Python
class Car:    def __init__(self, make, model, year):        self.make, self.model, self.year = make, model, year     def show(self):        print(f"{self.year} {self.make} {self.model}") Car("Mahindra", "XUV700", 2024).show()
Output
2024 Mahindra XUV700

Learn the concept

New to this topic? Read the lesson, then come back.

More oop programs

← All 48 OOP Programs

CheckingAccount with limit and fee

Python Practice › OOP Programs › Program 32

CheckingAccount with limit and fee

MediumOOP Programs · 32 of 48

Create CheckingAccount (inherits BankAccount) with transaction_limit and transaction_fee; check a withdrawal against the limit and deduct the fee.

Output
Withdrew 2000, fee 10, balance 7990
8000 exceeds the limit of 5000

Approach

  1. Reject amounts over the limit; otherwise subtract amount plus fee.

Solution

Python
class BankAccount:    def __init__(self, account_number, balance=0):        self.account_number, self.balance = account_number, balance class CheckingAccount(BankAccount):    def __init__(self, account_number, balance, transaction_limit, transaction_fee):        super().__init__(account_number, balance)        self.transaction_limit = transaction_limit        self.transaction_fee = transaction_fee     def withdraw(self, amount):        if amount > self.transaction_limit:            return f"{amount} exceeds the limit of {self.transaction_limit}"        self.balance -= amount + self.transaction_fee        return f"Withdrew {amount}, fee {self.transaction_fee}, balance {self.balance}" acc = CheckingAccount("C-9", 10000, 5000, 10)print(acc.withdraw(2000))print(acc.withdraw(8000))
Output
Withdrew 2000, fee 10, balance 7990
8000 exceeds the limit of 5000

Learn the concept

New to this topic? Read the lesson, then come back.

More oop programs

← All 48 OOP Programs

SavingsAccount with interest

Python Practice › OOP Programs › Program 31

SavingsAccount with interest

EasyOOP Programs · 31 of 48

Create SavingsAccount (inherits BankAccount) with interest_rate and minimum_balance, and a method to calculate interest.

Output
800.0
0

Approach

  1. Interest is earned only when the balance is at least the minimum.

Solution

Python
class BankAccount:    def __init__(self, account_number, balance=0):        self.account_number, self.balance = account_number, balance class SavingsAccount(BankAccount):    def __init__(self, account_number, balance, interest_rate, minimum_balance):        super().__init__(account_number, balance)        self.interest_rate = interest_rate        self.minimum_balance = minimum_balance     def interest(self):        if self.balance < self.minimum_balance:            return 0        return round(self.balance * self.interest_rate / 100, 2) print(SavingsAccount("S-1", 20000, 4, 5000).interest())print(SavingsAccount("S-2", 3000, 4, 5000).interest())
Output
800.0
0

Learn the concept

New to this topic? Read the lesson, then come back.

More oop programs

← All 48 OOP Programs

BankAccount class

Python Practice › OOP Programs › Program 30

BankAccount class

EasyOOP Programs · 30 of 48

Create a BankAccount class with account_number and balance, and methods to deposit and withdraw.

Output
AC-1001 1200
Error: insufficient funds

Approach

  1. Validate amounts in each method.

Solution

Python
class BankAccount:    def __init__(self, account_number, balance=0):        self.account_number = account_number        self.balance = balance     def deposit(self, amount):        if amount <= 0:            raise ValueError("deposit must be positive")        self.balance += amount     def withdraw(self, amount):        if amount > self.balance:            raise ValueError("insufficient funds")        self.balance -= amount acc = BankAccount("AC-1001", 1000)acc.deposit(500)acc.withdraw(300)print(acc.account_number, acc.balance)try:    acc.withdraw(5000)except ValueError as e:    print("Error:", e)
Output
AC-1001 1200
Error: insufficient funds

Learn the concept

New to this topic? Read the lesson, then come back.

More oop programs

← All 48 OOP Programs

Cat inherits from Animal

Python Practice › OOP Programs › Program 29

Cat inherits from Animal

EasyOOP Programs · 29 of 48

Create a Cat class that inherits from Animal (name, colour) and adds breed and weight.

Output
Milo | grey | Persian | 4.2 kg

Approach

  1. Extend the parent’s __init__ and add a show method.

Solution

Python
class Animal:    def __init__(self, name, color):        self.name, self.color = name, color class Cat(Animal):    def __init__(self, name, color, breed, weight):        super().__init__(name, color)        self.breed, self.weight = breed, weight     def show(self):        print(f"{self.name} | {self.color} | {self.breed} | {self.weight} kg") Cat("Milo", "grey", "Persian", 4.2).show()
Output
Milo | grey | Persian | 4.2 kg

Learn the concept

New to this topic? Read the lesson, then come back.

More oop programs

← All 48 OOP Programs