Library Management
Learn how to build a simple Library Management System using Python lists, dictionaries, functions, loops and search operations.
1. Project Introduction
A Library Management System can be used to store books and manage their availability.
In this project, we will practice adding books, viewing books, searching books, issuing books and returning books.
2. Creating Book Data
A dictionary can represent one book.
book = {
"id": 1,
"title": "Python Basics",
"author": "AIFA",
"available": True
}
3. Creating the Book List
Multiple books can be stored inside a list.
books = []
Every new book can be added to this list.
4. Adding a Book
book = {
"id": 1,
"title": "Python Basics",
"author": "AIFA",
"available": True
}
books.append(book)
5. Creating an Add Book Function
def add_book(book_id, title, author):
book = {
"id": book_id,
"title": title,
"author": author,
"available": True
}
books.append(book)
6. Viewing All Books
A for loop can display every book in the library.
for book in books:
print("ID:", book["id"])
print("Title:", book["title"])
print("Author:", book["author"])
print("Available:", book["available"])
print()
7. Searching for a Book
The title can be searched using a loop and string comparison.
search = input(
"Enter book title: "
).lower()
for book in books:
if search in book["title"].lower():
print(book)
8. Checking Book Availability
The Boolean value True or False can represent whether a book is currently available.
if book["available"]:
print("Book is available.")
else:
print("Book is already issued.")
9. Issuing a Book
When a book is issued, its availability changes.
if book["available"]:
book["available"] = False
print("Book issued successfully.")
else:
print("Book is not available.")
10. Returning a Book
Returning a book changes its availability back to True.
book["available"] = True
print("Book returned successfully.")
11. Finding a Book by ID
A unique book ID makes it easier to identify a specific book.
for book in books:
if book["id"] == book_id:
print(book)
12. Deleting a Book
Once the required book is found, it can be removed from the list.
for book in books:
if book["id"] == book_id:
books.remove(book)
break
13. Creating Library Functions
The project can be divided into separate functions:
- add_book()
- view_books()
- search_book()
- issue_book()
- return_book()
- delete_book()
14. Creating a Menu
while True:
print("1. Add Book")
print("2. View Books")
print("3. Search Book")
print("4. Issue Book")
print("5. Return Book")
print("6. Delete Book")
print("7. Exit")
choice = input(
"Enter choice: "
)
15. Using if / elif
if choice == "1":
add_book()
elif choice == "2":
view_books()
elif choice == "3":
search_book()
elif choice == "4":
issue_book()
elif choice == "5":
return_book()
elif choice == "6":
delete_book()
elif choice == "7":
break
16. Counting Available Books
A loop can be used to count currently available books.
available_count = 0
for book in books:
if book["available"]:
available_count += 1
17. Input Validation
if not title:
print("Title is required.")
elif not author:
print("Author is required.")
else:
add_book(
book_id,
title,
author
)
18. Complete Project Flow
19. Concepts You Practiced
- Python Lists
- Python Dictionaries
- Boolean Values
- for Loops
- while Loops
- if / elif / else
- Functions
- append()
- remove()
- String Methods
- Search Logic
- CRUD Operations
- Input Validation
20. Run Complete Project 🚀
You have learned the main concepts behind a Library Management System. Now open the complete interactive project.
Run Complete Project