0% found this document useful (0 votes)
89 views

OOPS in Python

The document discusses object-oriented programming concepts in Python like classes, objects, inheritance, polymorphism, encapsulation and more. It provides example code to create classes and objects, define methods, inherit from parent classes and override methods.

Uploaded by

ShriDikshanya
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
89 views

OOPS in Python

The document discusses object-oriented programming concepts in Python like classes, objects, inheritance, polymorphism, encapsulation and more. It provides example code to create classes and objects, define methods, inherit from parent classes and override methods.

Uploaded by

ShriDikshanya
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

P

Python - Lab Programs 7 – OOP in Python 1 © WISEN IT SOLUTIONS

Lab Programs 7

OOP in Python

Objectives

In this lab programs, you learn about

 Create object from the class


 Working the instance members
 Define & use the constructors
 Use the self keyword
 Return objects from methods
 Define & understand the concept of
polymorphism
 Define & understand the concept of inheritance
 Implement the concept of method overriding
 Define & understand the concept of
encapsulation
 Define & implement the nested classes
 Implement the recursion concept
 Understand the concept of garbage collection

Prerequisites

Before working on this lab program, you must know

 How to develop PY programs.


 How to declare variables.
 How to use literals.
 About the expressions & operators.
 About Functions

Estimated time to complete this lab programs: 150 minutes


© WISEN IT SOLUTIONS 2 Python - Lab Programs 7 – OOP in Python

 Lab Program 01

1. Open the LiClipse Python project called <Your-Name-Project.>


2. Create a package called CHP-08 in <Your-Enroll-Number>Project.
3. Create a new Python file called ClassVariableEx1.py in the CHP-08 Package.
4. Type the below code
class Person :
def __init__(self):
print("Initialization Method Called")
john = Person()
print(type(john))

5. Save the program.


6. Execute the program.

Program Output

What you learnt from this program?

 Lab Program 02

1. Create a new Python file called R01ObjectEx1.py in the CHP-08 Package.


2. Type the below code

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)

3. Save the program.


4. Execute the program.

Program Output

What you learnt from this program?

 Lab Program 03

1. Create a new Python file called R11InstanceVariablesEx1.py in the CHP-08 Package.


2. Type the below code

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)

3. Save the program.


4. Execute the program.

Program Output
© WISEN IT SOLUTIONS 4 Python - Lab Programs 7 – OOP in Python
What you learnt from this program?

 Lab Program 04

1. Create a new Python file called R21ParameterPassingEx1.py in the CHP-08 Package.


2. Type the below code

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)

3. Save the program.


4. Execute the program.

Program Output

What you learnt from this program?


P
Python - Lab Programs 7 – OOP in Python 5 © WISEN IT SOLUTIONS

 Lab Program 05

1. Create a new Python file called R31KeywordPassingEx1.py in the CHP-08 Package.


2. Type the below code

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)

3. Save the program.


4. Execute the program.

Program Output

What you learnt from this program?

 Lab Program 06

1. Create a new Python file called R41OptionalArgumentEx1.py in the CHP-08 Package.


2. Type the below code

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()

3. Save the program.


4. Execute the program.

Program Output

What you learnt from this program?

 Lab Program 07

1. Create a new Python file called R61InstanceMethodEx1.py in the CHP-08 Package.


2. Type the below code

class Demo:
def show(self):
print("Show Method Called")

def display(self):
print("Display Method Called")
self.show()
c1 = Demo()
c1.display()

3. Save the program.


4. Execute the program.
P
Python - Lab Programs 7 – OOP in Python 7 © WISEN IT SOLUTIONS

Program Output

What you learnt from this program?

 Lab Program 08

1. Create a new Python file called R71CallMethodFromAnotherMethodEx1.py in the CHP-08 Package.


2. Type the below code

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())

3. Save the program.


4. Execute the program.

Program Output
© WISEN IT SOLUTIONS 8 Python - Lab Programs 7 – OOP in Python
What you learnt from this program?

 Lab Program 09

1. Create a new Python file called R81ReturnObjectEx1.py in the CHP-08 Package.


2. Type the below code

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())

3. Save the program.


4. Execute the program.

Program Output

What you learnt from this program?


P
Python - Lab Programs 7 – OOP in Python 9 © WISEN IT SOLUTIONS

 Lab Program 10

1. Create a new Python file called R91ReturnObjectWithoutNoneEx1.py in the CHP-08 Package.


2. Type the below code

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)

3. Save the program.


4. Execute the program.

Program Output

What you learnt from this program?

 Lab Program 11

1. Create a new Python file called S01SimpleInheritanceEx1.py in the CHP-08 Package.


2. Type the below code

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()

3. Save the program.


4. Execute the program.

Program Output

What you learnt from this program?

 Lab Program 12

1. Create a new Python file called S11InheritanceMethodEx1.py in the CHP-08 Package.


2. Type the below code

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

3. Save the program.


4. Execute the program.

Program Output

What you learnt from this program?

 Lab Program 13

1. Create a new Python file called


S21AccessBaseClassvariableBeforeCallingBaseClassInitializationMethodEx1.py in the CHP-08
Package.
2. Type the below code

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()

3. Save the program.


4. Execute the program.
© WISEN IT SOLUTIONS 12 Python - Lab Programs 7 – OOP in Python
Program Output

What you learnt from this program?

 Lab Program 14

1. Create a new Python file called S31CallBaseClassInitializationMethodEx1.py in the CHP-08 Package.


2. Type the below code

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()

3. Save the program.


4. Execute the program.

Program Output
P
Python - Lab Programs 7 – OOP in Python 13 © WISEN IT SOLUTIONS

What you learnt from this program?

 Lab Program 15

1. Create a new Python file called S41DerivedClassMethosSameLikeSubClassMethodEx1.py in the CHP-08


Package.
2. Type the below code

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()

3. Save the program.


4. Execute the program.

Program Output

What you learnt from this program?


© WISEN IT SOLUTIONS 14 Python - Lab Programs 7 – OOP in Python

 Lab Program 16

1. Create a new Python file called S51InvokeSuperClassMethodEx1.py in the CHP-08 Package.


2. Type the below code

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()

3. Save the program.


4. Execute the program.

Program Output

What you learnt from this program?

 Lab Program 17

1. Create a new Python file called S61MultipleInheritaceEx1.py in the CHP-08 Package.


2. Type the below code
P
Python - Lab Programs 7 – OOP in Python 15 © WISEN IT SOLUTIONS

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))

3. Save the program.


4. Execute the program.

Program Output

What you learnt from this program?

 Lab Program 18

1. Create a new Python file called S71MultipleInheritanceCallSuperClassInitializationMethos.py in the CHP-


08 Package.
2. Type the below code

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)

3. Save the program.


4. Execute the program.

Program Output

What you learnt from this program?

 Lab Program 19

1. Create a new Python file called S81MultipleInheritanceCallSuperClassInitializationMethosEx1.py in the


CHP-08 Package.
2. Type the below code

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)

3. Save the program.


4. Execute the program.

Program Output

What you learnt from this program?

 Lab Program 20

1. Create a new Python file called S91MethodOverridingEx1.py in the CHP-08 Package.


2. Type the below code

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()

3. Save the program.


4. Execute the program.

Program Output

What you learnt from this program?

 Lab Program 21

1. Create a new Python file called T01EncapsulationEx1.py in the CHP-08 Package.


2. Type the below code

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()

3. Save the program.


4. Execute the program.

Program Output
P
Python - Lab Programs 7 – OOP in Python 19 © WISEN IT SOLUTIONS

What you learnt from this program?

 Lab Program 22

1. Create a new Python file called T11StaticVariableEx1.py in the CHP-08 Package.


2. Type the below code

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

What you learnt from this program?

 Lab Program 23

1. Create a new Python file called T21NestedClassEx1.py in the CHP-08 Package.


2. Type the below code

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()

3. Save the program.


4. Execute the program.

Program Output

What you learnt from this program?


P
Python - Lab Programs 7 – OOP in Python 21 © WISEN IT SOLUTIONS

 Lab Program 24

1. Create a new Python file called T31RecursionEx1.py in the CHP-08 Package.


2. Type the below code

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)

3. Save the program.


4. Execute the program.

Program Output

What you learnt from this program?

 Lab Program 25

1. Create a new Python file called T41DestructorCalledEx1.py in the CHP-08 Package.


2. Type the below code

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()

3. Save the program.


4. Execute the program.

Program Output

What you learnt from this program?

You might also like