OOPS in Python
OOPS in Python
Lab Programs 7
OOP in Python
Objectives
Prerequisites
Lab Program 01
Program Output
Lab Program 02
class Person :
def __init__(self):
self.uniqueID = -1
self.firstName = None
self.lastName = self.firstName
self.isActive = False
print("Initialization Method Called")
john = Person()
john.UniqueID = 101
john.FirstName = "John"
P
Python - Lab Programs 7 – OOP in Python 3 © WISEN IT SOLUTIONS
john.LastName = "Peter"
john.IsActive = True
print("John First Name is "+john.FirstName)
Program Output
Lab Program 03
class Student:
def __init__(self, studentID, firstName, lastName):
self.studentID = studentID
self.firstName = firstName
self.lastName = lastName
s1 = Student(101, "Mary", "Brown")
print(s1.lastName)
s2 = Student(102, "David", "Peter")
print(s2.lastName)
Program Output
© WISEN IT SOLUTIONS 4 Python - Lab Programs 7 – OOP in Python
What you learnt from this program?
Lab Program 04
class Student:
def __init__(self, studentID, firstName, lastName):
self.studentID = studentID
self.firstName = firstName
self.lastName = lastName
s1 = Student(studentID = 101, lastName= "Brown", firstName = "Mary")
print(s1.lastName)
s2 = Student(lastName= "Peter", firstName = "David", studentID = 102)
print(s2.lastName)
Program Output
Lab Program 05
class Student:
def __init__(self, studentID, firstName, lastName=""):
self.studentID = studentID
self.firstName = firstName
self.lastName = lastName
s1 = Student(101, "Mary")
print(s1.firstName+" "+s1.lastName)
s2 = Student(102, "David", 'Peter')
print(s2.firstName+" "+s2.lastName)
Program Output
Lab Program 06
class Person :
def __init__(self):
self.uniqueID = -1
self.firstName = None
self.lastName = None
self.isActive = False
print("Initialization Method Called")
© WISEN IT SOLUTIONS 6 Python - Lab Programs 7 – OOP in Python
def displayFullName(self):
print(self.firstName + " "+self.lastName)
john = Person()
john.firstName = "John"
john.lastName = "Peter"
john.displayFullName()
Program Output
Lab Program 07
class Demo:
def show(self):
print("Show Method Called")
def display(self):
print("Display Method Called")
self.show()
c1 = Demo()
c1.display()
Program Output
Lab Program 08
class Student:
def __init__(self, studentID, firstName, lastName):
self.studentID = studentID
self.firstName = firstName
self.lastName = lastName
def displayStudentInfo(self):
print(str(self.studentID)+" " + self.firstName+" " +self.lastName)
class College:
def getStudent(self):
s1 = Student(101, "Mary", "Brown")
return s1
c1 = College()
mary = c1.getStudent()
print(mary.displayStudentInfo())
Program Output
© WISEN IT SOLUTIONS 8 Python - Lab Programs 7 – OOP in Python
What you learnt from this program?
Lab Program 09
class Student:
def __init__(self, studentID, firstName, lastName):
self.studentID = studentID
self.firstName = firstName
self.lastName = lastName
def displayStudentInfo(self):
print(str(self.studentID)+" " + self.firstName+" " +self.lastName)
return ""
class College:
def getStudent(self):
s1 = Student(101, "Mary", "Brown")
return s1
c1 = College()
mary = c1.getStudent()
print(mary.displayStudentInfo())
Program Output
Lab Program 10
class A:
def __init__(self):
self.public = 10
self.__private = 30
def display(self):
print("Public Variable Data is "+str(self.public))
print("Private Variable Data is "+str(self.__private))
a1 = A()
a1.display()
print(a1.public)
print(a1.__private)
Program Output
Lab Program 11
class Point:
def __init__(self):
self.x = 10
def show(self):
print("Show Method Called")
© WISEN IT SOLUTIONS 10 Python - Lab Programs 7 – OOP in Python
class Line (Point) :
def __init__(self):
self.y = 10
def display(self):
print('Display Method Called')
l1 = Line()
l1.show()
l1.display()
Program Output
Lab Program 12
class Point:
def __init__(self):
self.x = 10
def show(self):
print("Show Method Called")
class Line (Point) :
def __init__(self):
self.y = 10
def display(self):
print('Display Method Called')
l1 = Line()
l1.show()
l1.display()
P
Python - Lab Programs 7 – OOP in Python 11 © WISEN IT SOLUTIONS
Program Output
Lab Program 13
class A:
def __init__(self):
self.baseData = 10
print("Super Class Init Called")
class B (A) :
def __init__(self):
self.subData = 20
print("Sub Class Init Called")
def display(self):
print('baseData is '+ str(self.baseData))
print('subData is '+ str(self.subData))
b1 = B()
b1.display()
Lab Program 14
class A:
def __init__(self):
self.baseData = 10
print("Super Class Init Called")
class B (A) :
def __init__(self):
self.subData = 20
print("Sub Class Init Called")
def display(self):
super().__init__()
print('baseData is '+ str(self.baseData))
print('subData is '+ str(self.subData))
b1 = B()
b1.display()
Program Output
P
Python - Lab Programs 7 – OOP in Python 13 © WISEN IT SOLUTIONS
Lab Program 15
class A:
def __init__(self):
print("Super Class Init Called")
def display(self):
print("Base Class Display Method Called")
class B (A) :
def __init__(self):
self.subData = 20
print("Sub Class Init Called")
def display(self):
print("Derived Class Display Method Called")
b1 = B()
b1.display()
Program Output
Lab Program 16
class A:
def __init__(self):
print("Super Class Init Called")
def display(self):
print("Base Class Display Method Called")
class B (A) :
def __init__(self):
self.subData = 20
print("Sub Class Init Called")
def display(self):
super().display()
print("Derived Class Display Method Called")
b1 = B()
b1.display()
Program Output
Lab Program 17
class A:
def __init__(self):
self.X = 0
class B:
def __init__(self):
self.Y = 0
class C(A,B):
def __init__(self):
self.Z = 0
c1 = C()
c1.X =10
c1.Y = 20
c1.Z = 30
print("X.Y.Z = "+str(c1.X)+"."+str(c1.Y)+"."+str(c1.Z))
Program Output
Lab Program 18
class A:
def __init__(self):
self.superClass1Data = "Super Class 1 Data"
© WISEN IT SOLUTIONS 16 Python - Lab Programs 7 – OOP in Python
class B:
def __init__(self):
self.superClass2Data = "Super Class 2 Data"
class C(A,B):
def __init__(self):
A.__init__(self)
B.__init__(self)
self.subClassData = "Sub Class Data"
c1 = C();
print(c1.superClass1Data)
print(c1.superClass2Data)
print(c1.subClassData)
Program Output
Lab Program 19
class A:
def __init__(self):
super(A, self).__init__()
self.superClass1Data = "Super Class 1 Data"
class B:
def __init__(self):
P
Python - Lab Programs 7 – OOP in Python 17 © WISEN IT SOLUTIONS
super(B, self).__init__()
self.superClass2Data = "Super Class 2 Data"
class C(A,B):
def __init__(self):
super(C, self).__init__()
self.subClassData = "Sub Class Data"
c1 = C();
print(c1.superClass1Data)
print(c1.superClass2Data)
print(c1.subClassData)
Program Output
Lab Program 20
class A:
def __init__(self):
print("Super Class Init Called")
def display(self):
print("Base Class Display Method Called")
class B (A) :
def __init__(self):
print("Sub Class Init Called")
def display(self):
print("Derived Class Display Method Called")
© WISEN IT SOLUTIONS 18 Python - Lab Programs 7 – OOP in Python
b1 = B()
b1.display()
Program Output
Lab Program 21
class A:
def __init__(self):
self.public = 10
self.__private = 20
def display(self):
print("Public Data is "+str(self.public))
print("Private Data is "+str(self.__private))
a1 = A()
a1.display()
a1.public = 30
a1.__private = 40
a1.display()
Program Output
P
Python - Lab Programs 7 – OOP in Python 19 © WISEN IT SOLUTIONS
Lab Program 22
class Student:
collegeName = ''
def __init__(self):
self.studentID = -1
self.studentName = None
def show(self):
pass;
s1 = Student()
s1.studentID = 101
s1.studentName = "Mary Brown"
Student.collegeName = "Eswari Engineering College"
print(s1.studentName+ " "+Student.collegeName)
s2 = Student()
s2.studentID = 101
s2.studentName = "David Peter"
print(s2.studentName+ " "+Student.collegeName)
3. Save the program.
4. Execute the program.
Program Output
© WISEN IT SOLUTIONS 20 Python - Lab Programs 7 – OOP in Python
Lab Program 23
class A:
def __init__(self):
print("A Initialization Method Called")
def display(self):
class B:
def __init__(self):
print("B Initialization Method Called")
b1 = B()
a1 = A()
a1.display()
Program Output
Lab Program 24
def Fibonacci(nbr) :
if (nbr == 0) :
return 0;
if (nbr == 1) :
return 1;
return Fibonacci(nbr - 1) + Fibonacci(nbr - 2);
result = Fibonacci(10)
print(result)
Program Output
Lab Program 25
class A:
def __init__(self):
print("A Initialization Method Called")
def __del__(self):
print("A Destruction Method Called")
© WISEN IT SOLUTIONS 22 Python - Lab Programs 7 – OOP in Python
a1 = A()
Program Output