Python Practice › OOP Programs
Python OOP Programs with Solutions
48 object-oriented programming exercises: constructors, instance and class variables, class and static methods, every kind of inheritance, overriding, abstract classes, encapsulation, properties and small real-world systems such as bank accounts, carts and courses.
Before you start
Read the Classes & Objects and Inheritance and Polymorphism & Encapsulation lesson first if the topic is new to you.
All programs
- #1Class with a constructor
Write a Python class with a constructor (__init__) that sets the object’s attributes.Easy - #2Instance variables
Write a Python class with instance variables and show that each object has its own values.Easy - #3Instance method
Write a Python class with an instance method that uses the object’s data.Easy - #4Class variables
Write a Python class with a class variable shared by all objects.Easy - #5Static method
Write a Python class with a static method.Easy - #6Class method
Write a Python class with a class method used as an alternative constructor.Easy - #7Class and module name
Write a Python program to get an object’s class name and module name.Easy - #8Create objects under if __name__ == "__main__"
Write a Python class and create its object inside theif __name__ == "__main__":block.Easy - #9Single inheritance
Write a Python program that demonstrates single inheritance.Easy - #10Multiple inheritance
Write a Python program that demonstrates multiple inheritance.Easy - #11Multilevel inheritance
Write a Python program that demonstrates multilevel inheritance.Easy - #12Hierarchical inheritance
Write a Python program that demonstrates hierarchical inheritance.Easy - #13Method overloading
Write a Python program that shows how to achieve method overloading.Medium - #14Method overriding
Write a Python program that demonstrates method overriding.Easy - #15Abstract method
Write a Python program with an abstract class and an abstract method.Easy - #16Encapsulation with a private attribute
Write a Python program that demonstrates encapsulation.Easy - #17School management system
Write a small school management system with classes for students and a school.Medium - #18Employee management system
Write a small employee management system with add, raise and report features.Medium - #19Property decorator
Write a Python class that uses@propertyfor a validated attribute.Easy - #20Class in a module, imported elsewhere
Write a Python class in its own module and import it from another script.Easy - #21Access five classes through one object
Create 5 classes and access them through a single class object (composition).Easy - #22Five-level inheritance chain
Create 5 classes and set up multilevel inheritance among them.Easy - #23setattr() and getattr()
Write a Python program that usessetattr()andgetattr().Easy - #24Protected and private members
Write a Python program showing protected (_x) and private (__x) members.Easy - #25Rectangle area class
Write a Python class to calculate the area of a rectangle.Easy - #26Circle area and circumference
Create aCircleclass with a radius and methods for area and circumference.Easy - #27Person class
Create aPersonclass with name and age and a method to print them.Easy - #28Student inherits from Person
Create aStudentclass that inherits fromPerson, adds student_id and grades, and prints name, age and id.Easy - #29Cat inherits from Animal
Create aCatclass that inherits fromAnimal(name, colour) and adds breed and weight.Easy - #30BankAccount class
Create aBankAccountclass with account_number and balance, and methods to deposit and withdraw.Easy - #31SavingsAccount with interest
CreateSavingsAccount(inheritsBankAccount) with interest_rate and minimum_balance, and a method to calculate interest.Easy - #32CheckingAccount with limit and fee
CreateCheckingAccount(inheritsBankAccount) with transaction_limit and transaction_fee; check a withdrawal against the limit and deduct the fee.Medium - #33Car class
Create aCarclass with make, model and year and a method to print them.Easy - #34ElectricCar with range
CreateElectricCar(inheritsCar) with battery_size and range_per_charge and a method to calculate the range.Easy - #35StudentRecord with average grade
CreateStudentRecordwith name, age and grades, with methods for the average and a summary.Easy - #36Course with students
CreateCoursewith name, teacher and students, with methods to add and remove students and print the course.Easy - #37Shape with Square and Triangle
Create aShapeclass with an area method and subclassesSquareandTriangle.Easy - #38Employee class
CreateEmployeewith name and salary and a method to print them.Easy - #39Manager with bonus
CreateManager(inheritsEmployee) with department and bonus, and a method for total compensation.Easy - #40Customer with deposit and withdraw
CreateCustomerwith name and balance and methods to deposit and withdraw.Easy - #41VIPCustomer with credit
CreateVIPCustomer(inheritsCustomer) with credit_limit and discount_rate, and a method for available credit.Easy - #42Phone class
CreatePhonewith brand, model and storage, with methods to call, text and check storage.Easy - #43Laptop class
CreateLaptopwith brand, model and storage, with methods to start up, shut down and check storage.Easy - #44Book class
CreateBookwith title, author and pages, with getter methods.Easy - #45EBook with open and close
CreateEBook(inheritsBook) with file_size and format, and methods to open and close the book.Easy - #46ShoppingCart class
CreateShoppingCartwith items and total cost, and methods to add and remove items and calculate the total.Easy - #47Animal class
CreateAnimalwith name and colour and a method to print them.Easy - #48Dog inherits from Animal
CreateDog(inheritsAnimal) with breed and weight, and a method to print all details.Easy