1.
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def display_info(self):
print(f"Name: {self.name}\nAge: {self.age}")
class Student(Person):
def __init__(self, name, age, student_id):
super().__init__(name, age)
self.student_id = student_id
def display_info(self):
super().display_info()
print(f"Student ID: {self.student_id}")
class Teacher(Person):
def __init__(self, name, age, employee_id):
super().__init__(name, age)
self.employee_id = employee_id
def display_info(self):
super().display_info()
print(f"Employee ID: {self.employee_id}")
student = Student("Sumithra S", 20, "S12345")
teacher = Teacher("Raghav K", 35, "T98765")
print("Student Information:")
student.display_info()
print("Teacher Information:")
teacher.display_info()
Output:
Student Information:
Name: Sumithra S
Age: 20
Student ID: S12345
Teacher Information:
Name: Raghav K
Age: 35
Employee ID: T98765
2.import math
class Shape:
def area(self):
pass
class Rectangle(Shape):
def __init__(self, length, width):
self.length = length
self.width = width
def area(self):
return self.length * self.width
class Circle(Shape):
def __init__(self, radius):
self.radius = radius
def area(self):
return math.pi *(self.radius ** 2)
rect = Rectangle(15, 7)
circ = Circle(8)
print(f"Rectangle area: {rect.area()}")
print(f"Circle area: {circ.area()}")
Output:
Rectangle area: 105
Circle area: 201.06192982974676
3.class Vehicle:
def __init__(self,model,make,year):
self.model=model
self.make=make
self.year=year
def display_model(self):
print(f"Model: {self.model}")
class Car(Vehicle):
def __init__(self,model,make,year,milege):
super().__init__(model,make,year)
self.milege=milege
def display_info(self):
print(f"Model: {self.model}")
print(f"make: {self.make}")
print(f"make: {self.year}")
print(f"make: {self.milege}")
class Motorcycle(Vehicle):
def __init__(self,make,model,year,color):
super().__init__(make,model,year)
self.color=color
def display_info(self):
print(f"make : {self.make}")
print(f"model : {self.model}")
print(f"year : {self.year}")
print(f"Color: {self.color}")
car1=Car("Baleno","Maruty",2020,15)
motorcycle1=Motorcycle("Pulsor","Honda",2001,"Black")
car1.display_info()
motorcycle1.display_info()
Output:
Model: Baleno
make: Maruty
make: 2020
make: 15
make : Honda
model : Pulsor
year : 2001
Color: Black
4. class Animal:
def __init__(self, name, species):
self.name = name
self.species = species
def make_sound (self):
pass
class Mammal(Animal):
def __init__ (self, name, species, fur_color):
super().__init__(name, species)
self.fur_color = fur_color
def give_birth(self):
print(f"{self.name} is giving birth.")
class Bird(Animal):
def __init__ (self, name, species, feather_color):
super().__init__ (name, species)
self.feather_color = feather_color
def lay_eggs(self):
print (f"{self.name} is laying eggs.")
lion = Mammal("Leo", "Lion", "Golden")
parrot= Bird("Aves", "Parrot", "Green")
print(f"{lion.name} is a {lion.species} with {lion.fur_color} fur.")
lion.give_birth()
print(f"{parrot.name} is a {parrot.species} with {parrot.feather_color} feathers.")
parrot.lay_eggs()
Output:
Leo is a Lion with Golden fur.
Leo is giving birth.
Aves is a Parrot with Green feathers.
Aves is laying eggs.
5. class Book:
def __init__(self, title, author, genre):
self.title = title
self.author = author
self.genre = genre
def display_info(self):
return f"{self.title} by {self.author} ({self.genre})"
class FictionBook (Book):
def __init__(self, title, author, subgenre):
super()._init_(title, author, genre="Fiction")
self.subgenre = subgenre
def display_info(self):
fiction_info = super().display_info()
return f"{fiction_info}, Subgenre: {self.subgenre}"
class NonFictionBook (Book):
def __init__(self, title, author, subject):
super()._init_ (title, author, genre="Non-Fiction")
self.subject = subject
def display_info(self):
non_fiction_info = super().display_info()
return f"{non_fiction_info}, Subject: {self.subject}"
class ScienceFictionBook(FictionBook):
def __init__ (self, title, author, subgenre, theme):
super()._init_(title, author, subgenre)
self.theme = theme
def display_info(self):
sci_fi_info = super().display_info()
return f"{sci_fi_info}, Theme: {self.theme}"
fiction_book = FictionBook("The Martian", "Andy Weir", "Science Fiction")
non_fiction_book = NonFictionBook( "Sapiens", "Yuval Noah Harari", "History")
sci_fi_book = ScienceFictionBook("Dune", "Frank Herbert", "Space Opera", "Exploration")
print(fiction_book.display_info())
print(non_fiction_book.display_info())
print(sci_fi_book.display_info())
Output:
The Martian by Andy Weir (Fiction
None, Subgenre: Science Fiction
Sapiens by Yuval Noah Harari (Non-Fiction
None, Subject: History
Dune by Frank Herbert (Fiction
None, Subgenre: Space Opera, Theme: Exploration