Book class

Python Practice › OOP Programs › Program 44

Book class

EasyOOP Programs · 44 of 48

Create Book with title, author and pages, with getter methods.

Output
Wings of Fire | A. P. J. Abdul Kalam | 180

Approach

  1. One method per attribute.

Solution

Python
class Book:    def __init__(self, title, author, pages):        self.title, self.author, self.pages = title, author, pages     def get_title(self):        return self.title     def get_author(self):        return self.author     def get_pages(self):        return self.pages b = Book("Wings of Fire", "A. P. J. Abdul Kalam", 180)print(b.get_title(), "|", b.get_author(), "|", b.get_pages())
Output
Wings of Fire | A. P. J. Abdul Kalam | 180
Tip

In idiomatic Python you would usually read b.title directly and add @property only when extra logic is needed.

Learn the concept

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

More oop programs

← All 48 OOP Programs

Laptop class

Python Practice › OOP Programs › Program 43

Laptop class

EasyOOP Programs · 43 of 48

Create Laptop with brand, model and storage, with methods to start up, shut down and check storage.

Output
Inspiron 15 started True
512 GB
Inspiron 15 shut down False

Approach

  1. Keep an is_on flag.

Solution

Python
class Laptop:    def __init__(self, brand, model, storage_gb):        self.brand, self.model, self.storage_gb = brand, model, storage_gb        self.is_on = False     def start(self):        self.is_on = True        return f"{self.model} started"     def shutdown(self):        self.is_on = False        return f"{self.model} shut down"     def storage(self):        return f"{self.storage_gb} GB" lp = Laptop("Dell", "Inspiron 15", 512)print(lp.start(), lp.is_on)print(lp.storage())print(lp.shutdown(), lp.is_on)
Output
Inspiron 15 started True
512 GB
Inspiron 15 shut down False

Learn the concept

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

More oop programs

← All 48 OOP Programs

Phone class

Python Practice › OOP Programs › Program 42

Phone class

EasyOOP Programs · 42 of 48

Create Phone with brand, model and storage, with methods to call, text and check storage.

Output
Calling 98765 43210 from Galaxy A55
Text to 98765 43210: Running late
81 GB free

Approach

  1. Track used storage and report what is free.

Solution

Python
class Phone:    def __init__(self, brand, model, storage_gb):        self.brand, self.model, self.storage_gb = brand, model, storage_gb        self.used_gb = 0     def call(self, number):        return f"Calling {number} from {self.model}"     def text(self, number, message):        return f"Text to {number}: {message}"     def free_storage(self):        return self.storage_gb - self.used_gb p = Phone("Samsung", "Galaxy A55", 128)p.used_gb = 47print(p.call("98765 43210"))print(p.text("98765 43210", "Running late"))print(p.free_storage(), "GB free")
Output
Calling 98765 43210 from Galaxy A55
Text to 98765 43210: Running late
81 GB free

Learn the concept

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

More oop programs

← All 48 OOP Programs

VIPCustomer with credit

Python Practice › OOP Programs › Program 41

VIPCustomer with credit

EasyOOP Programs · 41 of 48

Create VIPCustomer (inherits Customer) with credit_limit and discount_rate, and a method for available credit.

Output
12000 850.0

Approach

  1. Available credit = credit limit + balance.

Solution

Python
class Customer:    def __init__(self, name, balance=0):        self.name, self.balance = name, balance class VIPCustomer(Customer):    def __init__(self, name, balance, credit_limit, discount_rate):        super().__init__(name, balance)        self.credit_limit, self.discount_rate = credit_limit, discount_rate     def available_credit(self):        return self.balance + self.credit_limit     def price_after_discount(self, price):        return price * (1 - self.discount_rate / 100) v = VIPCustomer("Meena", 2000, 10000, 15)print(v.available_credit(), v.price_after_discount(1000))
Output
12000 850.0

Learn the concept

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

More oop programs

← All 48 OOP Programs

Customer with deposit and withdraw

Python Practice › OOP Programs › Program 40

Customer with deposit and withdraw

EasyOOP Programs · 40 of 48

Create Customer with name and balance and methods to deposit and withdraw.

Output
750 Insufficient balance 650

Approach

  1. Return a message from each method.

Solution

Python
class Customer:    def __init__(self, name, balance=0):        self.name, self.balance = name, balance     def deposit(self, amount):        self.balance += amount        return self.balance     def withdraw(self, amount):        if amount > self.balance:            return "Insufficient balance"        self.balance -= amount        return self.balance c = Customer("Asha", 500)print(c.deposit(250), c.withdraw(1000), c.withdraw(100))
Output
750 Insufficient balance 650

Learn the concept

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

More oop programs

← All 48 OOP Programs

Manager with bonus

Python Practice › OOP Programs › Program 39

Manager with bonus

EasyOOP Programs · 39 of 48

Create Manager (inherits Employee) with department and bonus, and a method for total compensation.

Output
Ravi QA 1350000

Approach

  1. Total = salary + bonus.

Solution

Python
class Employee:    def __init__(self, name, salary):        self.name, self.salary = name, salary class Manager(Employee):    def __init__(self, name, salary, department, bonus):        super().__init__(name, salary)        self.department, self.bonus = department, bonus     def total(self):        return self.salary + self.bonus m = Manager("Ravi", 1200000, "QA", 150000)print(m.name, m.department, m.total())
Output
Ravi QA 1350000

Learn the concept

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

More oop programs

← All 48 OOP Programs

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