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 Salamt', '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 Salamt', '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
/*54745756836*/