0% found this document useful (0 votes)
4 views2 pages

Python

The document outlines an assignment to create a class structure for books, including a base class 'Book' and subclasses 'EBook' and 'PhysicalBook' that implement inheritance. It also includes a generator function to print the Fibonacci series. The provided code demonstrates the implementation of these classes and functions.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views2 pages

Python

The document outlines an assignment to create a class structure for books, including a base class 'Book' and subclasses 'EBook' and 'PhysicalBook' that implement inheritance. It also includes a generator function to print the Fibonacci series. The provided code demonstrates the implementation of these classes and functions.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

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)

You might also like