April 23, 2025By

Object Oriented Programming in Python

A mini project on library management leveraging the concept of object oriented Programming in Python is given. Master the Python Programming Language with SevenMentor’s Python Course in Pune with Expert Training. Unlock Endless Career Opportunities

For Free, Demo classes Call: 02071171500

Registration Link: Click Here!

class Book:

    def __init__(self, title, author, ISBN):

        self.title = title

        self.author = author

        self.ISBN = ISBN

        self.is_available = True

    def __str__(self):

        return f”{self.title} by {self.author}”

    def check_availability(self):

        return self.is_available

    def borrow_book(self):

        if self.is_available:

            self.is_available = False

            return f”{self.title} has been borrowed.”

        else:

            return f”{self.title} is not available.”

    def return_book(self):

        if not self.is_available:

            self.is_available = True

            return f”{self.title} has been returned.”

        else:

            return f”{self.title} was not borrowed.”

Enroll Now

class User:

    def __init__(self, name):

        self.name = name

    def __str__(self):

        return self.name

class Library:

    def __init__(self):

        self.books = []  #list of instances of Book class

    def add_book(self, book):

        self.books.append(book)

    def remove_book(self, book):

        if book in self.books:

            self.books.remove(book)

    def list_books(self):

        book_strings = []  #list of books “Book by author”

        for book in self.books:

            book_strings.append(str(book))

        return “\n”.join(book_strings)

def main():

    library = Library()

    book1 = Book(“Wings of Fire”, “Dr.APJ Kalam and Arun Tiwari”, “978-8-173-711466”)

    book2 = Book(“Magic of Thinking Big”, “David Schwartz “, “978-1-78-504047-4”)

    library.add_book(book1)

    library.add_book(book2)

    user1 = User(“Abhay”)

    user2 = User(“Ram”)

    print(“Welcome to the Library Management System!”)

    print(“Available books:”)

    print(library.list_books())

    print(f”\n{user1} tries to borrow {book1}:”)

    print(book1.borrow_book())

    print(f”\n{user2} tries to borrow {book1}:”)

    print(book1.borrow_book())

    print(f”\n{user1} returns {book1}:”)

    print(book1.return_book())

    print(“\nAvailable books:”)

    print(library.list_books())

if __name__ == “__main__”:

    main()

**Output: **

Welcome to the Library Management System!

Available books:

Wings of Fire by Dr.APJ Kalam and Arun Tiwari

Magic of Thinking Big by David Schwartz 

Abhay tries to borrow Wings of Fire by Dr.APJ Kalam and Arun Tiwari:

Wings of Fire has been borrowed.

Ram tries to borrow Wings of Fire by Dr.APJ Kalam and Arun Tiwari:

Wings of Fire is not available.

Abhay returns Wings of Fire by Dr.APJ Kalam and Arun Tiwari:

Wings of Fire has been returned.

Available books:

Wings of Fire by Dr.APJ Kalam and Arun Tiwari

Magic of Thinking Big by David Schwartz 

Python Object-Oriented Programming Exercises, Solutions

Looking to sharpen your Python Object-Oriented Programming Exercises, Solutions. Explore our collection of exercises and solutions designed to enhance your understanding of OOP concepts. These hands-on exercises cover classes, objects, inheritance, and more, making Python OOP a breeze. Boost your coding prowess today.

  • 1. Write a Python program to create a person class. Include attributes like name, country and date of birth. Implement a method to determine the person’s age.


Solution :

Create a class Person with calculate_age

The output of the program:

Person 1

Name: Amitabh Bachchan

Country: Maharashtra

Date of Birth: 1942-11-11

Age: 82

  • 2. Write a Python program to create a class representing a bank. Include methods for managing customer accounts and transactions.


Solution :

Create a class bank with create_account.

A class bank with make_deposit.

A class bank with make_withdrawal.

A class bank with check_balance

Create an object of the class and execute all functions.

Output of the program:

  • 3. Write a Python program to create a class representing a shopping cart. Include methods for adding and removing items, and calculating the total price.


Solution: Create a class with functions add_item,remove_item, and calculate_total.

Create an object of a class and called functions of class.

The output of the program :

  • 4. Write a Python program to create a calculator class. Include methods for basic arithmetic operations.


Solution :

Create a class calculator with methods of adding, subtracting, multiply, and divide

Create an object of a class.

Called method using object.

One more function for handling an error.

The output of the program:

Thus we have seen some real-time application examples where we can use Object Oriented Concepts.

Note: Do watch our video on Demand of Python Programming

Expert trainer and consultant at SevenMentor with years of industry experience. Passionate about sharing knowledge and empowering the next generation of tech leaders.

All articles by Karishma Pawar

#Technology#Education#Career Guidance