ASSIGNMENT-4
1,2) Create a class to represent a book with attributes and
methods; Implement inheritance by creating subclasses for
different types of books.
#Base class
class Book:
def __init__(self, title, author, price):
[Link] = title
[Link] = author
[Link] = price
def get_description(self):
return f"'{[Link]}' by {[Link]}"
# Subclass for E-Books
class EBook(Book):
def __init__(self, title, author, price, file_size_mb):
super().__init__(title, author, price)
self.file_size_mb = file_size_mb
def get_description(self):
return f"{super().get_description()} [Digital - {self.file_size_mb}MB]"
# Subclass for Physical Books
class PhysicalBook(Book):
def __init__(self, title, author, price, weight_kg):
super().__init__(title, author, price)
self.weight_kg = weight_kg
def get_description(self):
return f"{super().get_description()} [Physical(Paperback)]"
digital = EBook("The Great Gatsby", "F. Scott Fitzgerald", 9.99, 2.5)
paperback = PhysicalBook("1984", "George Orwell", 15.00, 0.8)
print(digital.get_description())
print(paperback.get_description())
3) Write a generator function to generate the Fibonacci
series.
def fibonacci_print(n):
a, b = 0, 1
counter = 0
while counter < n:
print(a, end=" ")
a, b = b, a + b
counter += 1
fibonacci_print(10)