SE Computer Engineering Practical 16 solution
Question:
Write a Python program for department library which has N books, write functions for following:
a) Delete the duplicate entries
b) Display books in ascending order based on cost of books
c) Count number of books with cost more than 500.
d) Copy books in a new list which has cost less than 500.
Code:
def delete_duplicates(books):
unique_books = list(set(books))
return unique_books
def display_books_in_ascending_order(books):
sorted_books = sorted(books, key=lambda x: x[2])
return sorted_books
def count_books_more_than_500(books):
count = sum(1 for book in books if book[2] > 500)
return count
def copy_books_cost_less_than_500(books):
less_than_500 = [book for book in books if book[2] < 500]
return less_than_500
if __name__ == "__main__":
N = int(input("Enter the number of books: "))
books = []
for i in range(N):
print(f"\nEnter details for book {i+1}:")
name = input("Enter the book name: ")
author = input("Enter the author name: ")
cost = float(input("Enter the cost of the book: "))
books.append((name, author, cost))
# a) Delete duplicate entries
books = delete_duplicates(books)
print("\nAfter removing duplicate entries:")
for book in books:
print(book)
# b) Display books in ascending order based on cost
sorted_books = display_books_in_ascending_order(books)
print("\nBooks in ascending order based on cost:")
for book in sorted_books:
print(book)
# c) Count number of books with cost more than 500
count_more_than_500 = count_books_more_than_500(books)
print("\nNumber of books with cost more than 500:", count_more_than_500)
# d) Copy books with cost less than 500 to a new list
less_than_500_books = copy_books_cost_less_than_500(books)
print("\nBooks with cost less than 500:")
for book in less_than_500_books:
print(book)
Output:
Enter the number of books: 7 Enter details for book 1: Enter the book name: “Queen of Fire” Enter the author name: Devika Rangachari Enter the cost of the book: 450 Enter details for book 2: Enter the book name: ‘The India Story’ Enter the author name: Bimal Jalal Enter the cost of the book: 500 Enter details for book 3: Enter the book name: ‘A Place Called Home’ Enter the author name: Preeti Shenoy Enter the cost of the book: 750 Enter details for book 4: Enter the book name: Lal Salam Enter the author name: Smriti Irani Enter the cost of the book: 800 Enter details for book 5: Enter the book name: ‘Hear Yourself’ Enter the author name: Indian Author Prem Rawat Enter the cost of the book: 300 Enter details for book 6: Enter the book name: ‘The Queen of Indian Pop’ Enter the author name: Usha Uthup Enter the cost of the book: 550 Enter details for book 7: Enter the book name: ‘Golden Boy Neeraj Chopra’ Enter the author name: Navdeep Singh Gill Enter the cost of the book: 750 After removing duplicate entries: ('‘A Place Called Home’\t', 'Preeti Shenoy', 750.0) ('“Queen of Fire”\t', 'Devika Rangachari', 450.0) ('‘Hear Yourself’\t', 'Indian Author Prem Rawat', 300.0) ('‘The Queen of Indian Pop’\t', 'Usha Uthup', 550.0) ('Lal Salam\t', 'Smriti Irani', 800.0) ('‘The India Story’\t', 'Bimal Jalal', 500.0) ('‘Golden Boy Neeraj Chopra’\t', 'Navdeep Singh Gill', 750.0) Books in ascending order based on cost: ('‘Hear Yourself’\t', 'Indian Author Prem Rawat', 300.0) ('“Queen of Fire”\t', 'Devika Rangachari', 450.0) ('‘The India Story’\t', 'Bimal Jalal', 500.0) ('‘The Queen of Indian Pop’\t', 'Usha Uthup', 550.0) ('‘A Place Called Home’\t', 'Preeti Shenoy', 750.0) ('‘Golden Boy Neeraj Chopra’\t', 'Navdeep Singh Gill', 750.0) ('Lal Salam\t', 'Smriti Irani', 800.0) Number of books with cost more than 500: 4 Books with cost less than 500: ('“Queen of Fire”\t', 'Devika Rangachari', 450.0) ('‘Hear Yourself’\t', 'Indian Author Prem Rawat', 300.0) Process finished with exit code 0