1. Create a library of books in Python, with the following attributes.
(i) Title of book
(ii) Author of book
(iii) ISBN (International Standard Book Number)
(iv) Year of Publication.
(v) Book Classification (e.g. Fiction, Science, Art)
class Book:
def __init__(self, title, author, isbn=None, year=None, rubric=None):
self._title = title
self._author = author
self._isbn = isbn
self._year = year
self._class = rubric
def __str__(self):
res = "Title: " + self._title
res += "\nAuthor: " + self._author
if self._isbn:
res += "\nISBN: " + str(self._isbn)
if self._year:
res += "\nYear: " + str(self._year)
if self._class:
res += "\nClass: " + self._class
res += '\n'
return res
def main():
library = []
library.append(Book("Harry Potter and the Sorcerer's Stone",
"J.K.Rowling", year=1998, rubric="fantasy"))
library.append(Book("The Art of Computer Programming, vol.1", "Donald E.Knuth",
9780201896831, 1998, "scince"))
library.append(Book("The Louvre: All the Paintings Paperback", "Erich Lessing",
year=2020, rubric="art"))
library.append(Book("The Ultimate Hitchhiker's Guide", "Douglas Adams",
year=1996))
for book in library:
print(book)
if __name__ == '__main__':
main()
Comments
Leave a comment