Python Practice › Function Programs › Program 33
Library management system
Write a Python program with functions to add, issue, return and list books in a library.
Output
Issued 'Clean Code' 'Clean Code' is not available Returned 'Clean Code' Available: ['Python Basics', 'Clean Code', 'Atomic Habits']
Approach
- Store each book’s availability in a dictionary.
Solution
books = {} def add_book(title): books[title] = True # True means available def issue(title): if books.get(title): books[title] = False return f"Issued '{title}'" return f"'{title}' is not available" def return_book(title): if title in books and not books[title]: books[title] = True return f"Returned '{title}'" return f"'{title}' was not issued" def available(): return [t for t, ok in books.items() if ok] for t in ["Python Basics", "Clean Code", "Atomic Habits"]: add_book(t)print(issue("Clean Code"))print(issue("Clean Code"))print(return_book("Clean Code"))print("Available:", available())Output
Issued 'Clean Code' 'Clean Code' is not available Returned 'Clean Code' Available: ['Python Basics', 'Clean Code', 'Atomic Habits']
Line by line
Lines 6-9
A book can be issued only while it is available.
Lines 12-15
Returning works only for books that were issued.
Learn the concept
New to this topic? Read the lesson, then come back.