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

Student inherits from Person

Python Practice › OOP Programs › Program 28

Student inherits from Person

EasyOOP Programs · 28 of 48

Create a Student class that inherits from Person, adds student_id and grades, and prints name, age and id.

Output
Ravi, 17, ID S-104

Approach

  1. Call super().__init__ for the shared attributes.

Solution

Python
class Person:    def __init__(self, name, age):        self.name, self.age = name, age class Student(Person):    def __init__(self, name, age, student_id, grades):        super().__init__(name, age)        self.student_id = student_id        self.grades = grades     def show(self):        print(f"{self.name}, {self.age}, ID {self.student_id}") Student("Ravi", 17, "S-104", [88, 92]).show()
Output
Ravi, 17, ID S-104

Line by line

Line 7

The parent sets name and age, so that code is not repeated.

Learn the concept

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

More oop programs

← All 48 OOP Programs

Person class

Python Practice › OOP Programs › Program 27

Person class

EasyOOP Programs · 27 of 48

Create a Person class with name and age and a method to print them.

Output
Name: Asha, Age: 29

Approach

  1. Store the attributes and print them in a method.

Solution

Python
class Person:    def __init__(self, name, age):        self.name = name        self.age = age     def show(self):        print(f"Name: {self.name}, Age: {self.age}") Person("Asha", 29).show()
Output
Name: Asha, Age: 29

Learn the concept

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

More oop programs

← All 48 OOP Programs

Circle area and circumference

Python Practice › OOP Programs › Program 26

Circle area and circumference

EasyOOP Programs · 26 of 48

Create a Circle class with a radius and methods for area and circumference.

Output
153.94 43.98

Approach

  1. Use math.pi.

Solution

Python
import math class Circle:    def __init__(self, radius):        self.radius = radius     def area(self):        return round(math.pi * self.radius ** 2, 2)     def circumference(self):        return round(2 * math.pi * self.radius, 2) c = Circle(7)print(c.area(), c.circumference())
Output
153.94 43.98

Learn the concept

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

More oop programs

← All 48 OOP Programs

Rectangle area class

Python Practice › OOP Programs › Program 25

Rectangle area class

EasyOOP Programs · 25 of 48

Write a Python class to calculate the area of a rectangle.

Output
40

Approach

  1. Store length and width; return their product.

Solution

Python
class Rectangle:    def __init__(self, length, width):        self.length = length        self.width = width     def area(self):        return self.length * self.width print(Rectangle(8, 5).area())
Output
40

Learn the concept

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

More oop programs

← All 48 OOP Programs