Python
Python

 

Introduction

A Library Management System is designed to help library administrators and members manage the resources effectively. The system helps in managing the books, adding and removing them, managing member registrations, and tracking book checkouts and returns. In this program, we will build a simple Library Management System using Python that will handle the following functionalities:

  • Manage books in the library (add, remove, search)
  • Manage members (add, remove, view details)
  • Check in and check out books for members

Objective

The objective of this project is to create a simple, easy-to-understand Library Management System in Python. It will provide functionalities to manage books, library members, and book transactions, which are common operations in real-world library management. This system will serve as a foundational project for beginners in Python.

Code Implementation


# Library Management System in Python

class Book:
    def __init__(self, title, author, isbn):
        self.title = title
        self.author = author
        self.isbn = isbn
        self.available = True

    def __str__(self):
        return f"Title: {self.title}, Author: {self.author}, ISBN: {self.isbn}, Available: {self.available}"

class Member:
    def __init__(self, name, member_id):
        self.name = name
        self.member_id = member_id
        self.books_checked_out = []

    def check_out_book(self, book):
        if book.available:
            book.available = False
            self.books_checked_out.append(book)
            print(f"{book.title} has been checked out to {self.name}.")
        else:
            print(f"Sorry, {book.title} is not available right now.")

    def return_book(self, book):
        if book in self.books_checked_out:
            book.available = True
            self.books_checked_out.remove(book)
            print(f"{book.title} has been returned by {self.name}.")
        else:
            print(f"{self.name} did not check out {book.title}.")

    def __str__(self):
        return f"Member Name: {self.name}, Member ID: {self.member_id}"

class Library:
    def __init__(self):
        self.books = []
        self.members = []

    def add_book(self, book):
        self.books.append(book)

    def remove_book(self, book):
        if book in self.books:
            self.books.remove(book)
            print(f"{book.title} has been removed from the library.")
        else:
            print("Book not found.")

    def add_member(self, member):
        self.members.append(member)

    def remove_member(self, member):
        if member in self.members:
            self.members.remove(member)
            print(f"{member.name} has been removed from the library members.")
        else:
            print("Member not found.")

    def search_book(self, title):
        for book in self.books:
            if title.lower() in book.title.lower():
                return book
        return None

    def __str__(self):
        return f"Library with {len(self.books)} books and {len(self.members)} members."

# Driver code
def main():
    library = Library()

    # Adding books to the library
    book1 = Book("Python Programming", "John Doe", "12345")
    book2 = Book("Data Science with Python", "Jane Smith", "67890")
    library.add_book(book1)
    library.add_book(book2)

    # Adding members to the library
    member1 = Member("Alice", 1)
    member2 = Member("Bob", 2)
    library.add_member(member1)
    library.add_member(member2)

    # Checking out books
    member1.check_out_book(book1)
    member2.check_out_book(book2)

    # Returning books
    member1.return_book(book1)
    member2.return_book(book2)

    # Searching for a book
    search_result = library.search_book("Python Programming")
    if search_result:
        print(f"Book found: {search_result}")
    else:
        print("Book not found.")

if __name__ == "__main__":
    main()
            

Explanation of Program Structure

The program consists of three main classes: Book, Member, and Library.

  • Book Class: Manages individual book details, such as title, author, ISBN, and availability.
  • Member Class: Manages library member details and their book checkouts and returns.
  • Library Class: Manages the collection of books and members. It allows adding and removing books, searching for books, and managing members.

The main function of the program simulates a simple library workflow. It first creates a library, adds books and members, then simulates checking out and returning books. It also searches for a book by title and displays its details if found.

How to Run the Program

1. Copy the code into a Python file (e.g., library_management.py).

2. Open a terminal or command prompt and navigate to the directory where the Python file is located.

3. Run the program by executing the command:

python library_management.py

4. The program will simulate adding books, checking out and returning books, and searching for a book in the library.

© 2025 Learn Programming. All Rights Reserved.

 

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

Your email address will not be published. Required fields are marked *

error

Enjoy this blog? Please spread the word :)