pythonhindii logo

Python Hindi

Python in Hindi | Programming Education

Python Library Management System Project

python-library-management-system

Python Library Management System Project (Source Code + Explanation)

Is post me hum Python me ek simple Library Management System banayenge, jisme hum books add, view, issue aur return kar sakte hain, data JSON file me save hota hai.

Project Overview

Ye project beginners ke liye best hai jo Python me small CLI (console) application banana chahte hain aur sath me file handling, list, dictionary aur functions ka use samajhna chahte hain.

Data Structure Design

  • List : saari books ko store karegi.
  • Dictionary : har book ka title aur issued status rakhegi.
  • Boolean Flag : issued = True/False se availability track hogi.
  • JSON File : library.json me data persist hoga.

Features

  • New book add karna.
  • Saari books list / view karna.
  • Book issue karna (agar available ho).
  • Book return karna (agar pehle issued ho).
  • Program band karte time data file me save ho jata hai.

Learning Points

  • State manage karna using True/False flags.
  • Multi‑action menu driven system banana.
  • Real‑world logic (availability check, invalid input handling).
  • Structured Python application design (functions + file handling).

Python Source Code


import json
library = []

def add_book():
    title = input("Enter book title: ")
    library.append({"title": title, "issued": False})
    print("Book added")

def show_books():
    if not library:
        print("No books available")
    else:
        print("\nLibrary Books:")
        for i, book in enumerate(library):
            status = "Issued" if book["issued"] else "Available"
            print(f"{i+1}. {book['title']} - {status}")

def issue_book():
    show_books()
    try:
        index = int(input("Enter book number to issue: ")) - 1
        if 0 <= index < len(library):
            if not library[index]["issued"]:
                library[index]["issued"] = True
                print("Book issued")
            else:
                print("Book already issued")
        else:
            print("Invalid choice")
    except:
        print("Enter valid number")

def return_book():
    show_books()
    try:
        index = int(input("Enter book number to return: ")) - 1
        if 0 <= index < len(library):
            if library[index]["issued"]:
                library[index]["issued"] = False
                print("Book returned")
            else:
                print("Book was not issued")
        else:
            print("Invalid choice")
    except:
        print("Enter valid number")

def save_data():
    with open("library.json", "w") as file:
        json.dump(library, file)

def load_data():
    try:
        with open("library.json", "r") as file:
            data = json.load(file)
        library.extend(data)
    except:
        pass

load_data()

while True:
    print("\n1. Add Book")
    print("2. View Books")
    print("3. Issue Book")
    print("4. Return Book")
    print("5. Exit")
    choice = input("Enter choice: ")
    if choice == "1":
        add_book()
    elif choice == "2":
        show_books()
    elif choice == "3":
        issue_book()
    elif choice == "4":
        return_book()
    elif choice == "5":
        save_data()
        print("Data saved. Exiting...")
        break
    else:
        print("Invalid choice")