Python Practical Unit 3 Final
Python Practical Unit 3 Final
MANESH PATEL
PRESIDENT INSTITUTE OF COMPUTER APPLICAION COLLEGE, SHAYONA CAMPUS, A’BAD.
class Student:
# Constructor with multiple parameters
def __init__(self, name, roll_no, course):
self.name = name
self.roll_no = roll_no
self.course = course
def display(self):
print("Student Details:")
print(f"Name = {S1.name}")
print(f"Roll No = {self.roll_no}")
print(f"Course = {S1.course}")
# Create an object
S1 = Student("Manesh Patel", 10, "BCA")
S1.display()
Student Details:
Name = Manesh Patel
Roll No = 10
Course = BCA
PREPARED BY: PATEL MANESH - M.Sc(CA & IT) Contact: 90165 17796 1
PATEL MANESH
class Student:
# Class (Static) Variable
clg_name = "BCA College"
def display(self):
print(f"Name= {self.name}, Roll No= {self.roll_no}, College= {Student.clg_name}")
s1.display()
s2.display()
print()
print("College Name = ", Student.clg_name)
print()
s
PREPARED BY: PATEL MANESH - M.Sc(CA & IT), B.ED Contact: 90165 17796 2
PATEL MANESH
class Student:
def __init__(self):
# Private instance variables
self.name = ""
self.roll_no = 0
def get_roll_no(self):
return self.roll_no
s
PREPARED BY: PATEL MANESH - M.Sc(CA & IT), B.ED Contact: 90165 17796 3
PATEL MANESH
class Student:
@classmethod
def update_college(cls, new_college):
cls.college_name = new_college
def display(self):
print("Name= ", self.name)
print("Roll No= ",self.roll_no)
print("College= ", Student.college_name)
print()
# Create objects
s1 = Student("Nidhi", 111)
s2 = Student("Payal", 222) Name= Nidhi
Roll No= 111
s1.display() College= BCA College
s2.display()
Name= Payal
Student.update_college("BBA College") Roll No= 222
College= BCA College
s1.display()
s2.display() Name= Nidhi
Roll No= 111
College= BBA College
Name= payal
Roll No= 222
College= BBA College
s
PREPARED BY: PATEL MANESH - M.Sc(CA & IT), B.ED Contact: 90165 17796 4
PATEL MANESH
class Student:
count = 0 # class variable to count number of objects
def __init__(self):
Student.count = self.count + 1
def display():
print("Total Objects are created= ", Student.count)
s1 = Student()
s2 = Student()
s3 = Student()
s4 = Student()
Total Objects are created= 4
Student.display()
class Bank:
def __init__(self, name, balance):
self.name = name
self.balance = balance
def show(manish):
print("Name= ", manish.name)
print("Balance= ", manish.balance)
print()
# Create an object
acc = Bank("Nivanshi", 1000)
Name= Nivanshi
acc.show() Balance= 1000
class Student:
def __init__(self):
self._id = " "
self._name = ""
self._marks = None
s
PREPARED BY: PATEL MANESH - M.Sc(CA & IT), B.ED Contact: 90165 17796 6
PATEL MANESH
def get_id(self):
return self._id
def get_name(self):
return self._name
def get_marks(self):
return self._marks
s1.set_id(11)
s1.set_name("Ashvin Patel")
s1.set_marks(75)
Student ID= 11
Student Name= Ashvin Patel
Student Marks= 75
s
PREPARED BY: PATEL MANESH - M.Sc(CA & IT), B.ED Contact: 90165 17796 7
PATEL MANESH
class Student:
def __init__(self, name, rollno):
self.name = name
self.rollno = rollno
def display(self):
print("Name=", self.name)
print("Roll no=", self.rollno)
# Subclass
class Child(Student):
def __init__(self, name, rollno):
super().__init__(name, rollno)
def get(self):
super().display()
# Create an object
C = Child("Manish", 50)
C.get()
Name= Manish
Roll no= 50
s
PREPARED BY: PATEL MANESH - M.Sc(CA & IT), B.ED Contact: 90165 17796 8
PATEL MANESH
class Person:
def __init__(self, name):
self.name = name
def show_name(self):
print("Name= ", self.name)
# First subclass
class Doctor(Person):
def __init__(self, name, Degree):
super().__init__(name)
self.Degree = Degree
def show(self):
self.show_name()
print("Degree= ", self.Degree)
# Second subclass
class Engineer(Person):
def __init__(self, name, field):
super().__init__(name)
self.field = field
def show(self):
self.show_name()
print("Engineering Field= ", self.field)
# Create objects
doc = Doctor("Dr. Vipul", "MBBS")
eng = Engineer("Ashvin", "Copmuter")
s
PREPARED BY: PATEL MANESH - M.Sc(CA & IT), B.ED Contact: 90165 17796 9
PATEL MANESH
Engineer Information==
Name= Ashvin
Engineering Field= Computer
def __init__(self):
self.father_name = "Manish Patel"
def __init__(self):
self.mother_name = "Bina Patel"
def __init__(self):
Father.__init__(self)
Mother.__init__(self)
self.child_name = "Nivanshi"
s
PREPARED BY: PATEL MANESH - M.Sc(CA & IT), B.ED Contact: 90165 17796 10
PATEL MANESH
def show(ABC):
class A:
def show(self):
print("Method from class A")
class B(A):
def show(self):
print("Method from class B")
class C(A):
def show(self):
print("Method from class C")
s
PREPARED BY: PATEL MANESH - M.Sc(CA & IT), B.ED Contact: 90165 17796 11
PATEL MANESH
d = D()
d.show()
# Print MRO
s
PREPARED BY: PATEL MANESH - M.Sc(CA & IT), B.ED Contact: 90165 17796 12
PATEL MANESH
class Student:
def study(self):
print("Student is studying")
class Teacher:
def teach(self):
print("Teacher is teaching")
# Create objects
s = Student()
t = Teacher()
if hasattr(s, 'teach'):
print("Method 'teach' exists in Student object.")
s.teach()
else:
print("Method 'teach' does NOT exist in Student object.")
s
PREPARED BY: PATEL MANESH - M.Sc(CA & IT), B.ED Contact: 90165 17796 13
PATEL MANESH
class Addition:
def __init__(self, x, y):
self.p = x
self.q = y
def display(self):
print("X (10 + 30) = ", self.p)
print("Y (20 + 40)= ", self.q)
s
PREPARED BY: PATEL MANESH - M.Sc(CA & IT), B.ED Contact: 90165 17796 14
PATEL MANESH
class Demo:
D = Demo()
s
PREPARED BY: PATEL MANESH - M.Sc(CA & IT), B.ED Contact: 90165 17796 15
PATEL MANESH
class Father:
def show(self):
print("I am from Father Class")
class Child(Father):
def show(self):
#super().show()
print("I am from Child Class")
s
PREPARED BY: PATEL MANESH - M.Sc(CA & IT), B.ED Contact: 90165 17796 16
PATEL MANESH
try:
a = int(input("Enter No1= "))
b = int(input("Enter No2= "))
# Division operation
result = a / b
print("Result = ", result)
except ZeroDivisionError:
print("Error: Cannot divide by zero.")
except ValueError:
print("Error: Please enter valid integers only.")
except Exception as e:
print("An unexpected error occurred:", e)
finally:
print("Execution completed.")
Enter No1= 10
Enter No2= 2
Result = 5.0
Execution completed.
Enter No1= 10
Enter No2= 0
Error: Cannot divide by zero.
Execution completed.
Enter No1= 50
Enter No2= manish
Error: Please enter valid integers only.
Execution completed.
s
PREPARED BY: PATEL MANESH - M.Sc(CA & IT), B.ED Contact: 90165 17796 17
PATEL MANESH
try:
a = int(input("Enter No1= "))
b = int(input("Enter No2= "))
# Division operation
result = a / b
print("Result = ", result)
except ZeroDivisionError:
print("Error: Cannot divide by zero.")
except ValueError:
print("Error: Please enter valid integers only.")
except Exception as e:
print("An unexpected error occurred:", e)
finally:
print("Execution completed.")
s
PREPARED BY: PATEL MANESH - M.Sc(CA & IT), B.ED Contact: 90165 17796 18