class Student: # Default constructor def init(self): self.a = input("Enter the student's name: ")
# Method to display message
def show(self):
print("This is non-parameterized constructor")
print("Welcome", self.a, "!")
s1 = Student() s1.show() Enter the student's name: Pika This is non-parameterized constructor Welcome Pika !
class Demo: # Constructor def init(self): self.status = "Alive" print("Object is created.") print("Status:", self.status)
# Destructor
def __del__(self):
print("Destructor is called. Object is being destroyed.")
obj = Demo()
del obj
print("End of the program.") Object is created. Status: Alive Destructor is called. Object is being destroyed. End of the program.
class Details: def init(self, name, age): self.name = name self.age = age
def getName(self):
return self.name
def getAge(self):
return self.age
class Employee(Details): def init(self, name, age, employee_id, department): super().init(name, age) self.employee_id = employee_id self.department = department
def getEmployeeDetails(self):
print("\n--- Employee Details ---")
print("Name:", self.getName())
print("Age:", self.getAge()
Enter Employee Details: Enter Name: John Enter Age: 30 Enter Employee ID: E101 Enter Department: HR
Enter Patient Details: Enter Name: Alice Enter Age: 25 Enter Patient ID: P205 Enter Disease: Fever
--- Employee Details --- Name: John Age: 30 Employee ID: E101 Department: HR
--- Patient Details --- Name: Alice Age: 25 Patient ID: P205 Disease: Fever
class Person: def init(self, name): self.name = name
def getName(self):
return self.name
class Age(Person): def init(self, name, age): super().init(name) self.age = age
def getAge(self):
return self.age
class Location(Age): def init(self, name, age, location): super().init(name, age) self.location = location
def getLocation(self):
return self.location
name = input("Enter Name: ") age = int(input("Enter Age: ")) location = input("Enter Location: ")
person1 = Location(name, age, location)
print("\n--- Person Details ---") print("Name:", person1.getName()) print("Age:", person1.getAge()) print("Location:", person1.getLocation()) [10/22, 3:51 PM] Bushpika Python Class: Enter Name: Sarah Enter Age: 28 Enter Location: Chennai
--- Person Details --- Name: Sarah Age: 28 Location: Chennai [10/22, 3:54 PM] Bushpika Python Class: # Class 1: For Addition class Calculation1: def Summation(self, a, b): return a + b
class Calculation2: def Subtraction(self, a, b): return a - b
class Derived(Calculation1, Calculation2): def Division(self, a, b): if b == 0: return "Division by zero is not allowed" return a / b
a = float(input("Enter first number: ")) b = float(input("Enter second number: "))
obj = Derived()
sum_result = obj.Summation(a, b) sub_result = obj.Subtraction(a, b) div_result = obj.Division(a, b)
print("\n--- Arithmetic Operations Result ---") print(f"Addition of {a} and {b} = {sum_result}") print(f"Subtraction of {a} and {b} = {sub_result}") print(f"Division of {a} and {b} = {div_result}") Enter first number: 20 Enter second number: 5
--- Arithmetic Operations Result --- Addition of 20.0 and 5.0 = 25.0 Subtraction of 20.0 and 5.0 = 15.0 Division of 20.0 and 5.0 = 4.0