DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
WORKSHEET – 3.2
Student Name:Aditi UID: 21BCS2159
Branch: CSE Section/Group: 21BCS605-B
Semester:4 Date of Performance:17/04/2023
Subject Code: 21CSP-259
Subject Name: Programming in Python Lab
1. AIM: Program to implement concept of object oriented programming such as
classes, inheritance and polymorphism.
a) Write a Python class named Student with two attributes student_id,
student_name. Add a new attribute student_class and display the entire
attribute and their values of the said class. Now remove the student_name
attribute and display the entire attribute with values.
PROGRAM CODE –
class Student:
def __init__(self, student_id, student_name):
self.student_id = student_id
self.student_name = student_name
student = Student(2159, "ADITI")
student.student_class = "605"
print(student.__dict__)
del student.student_name
print(student.__dict__)
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
student1.remove_name()
student1.display_attributes()
OUTPUT –
b) Write a Python class to find a pair of elements (indices of the two numbers)
from a given array whose sum equals a specific target number.
PROGRAM CODE –
class TwoSum:
def init (self, nums, target):
[Link] = nums
[Link] = target
def find_indices(self):
index_map = {}
for i, num in enumerate([Link]):
complement = [Link] - num
if complement in index_map:
return [index_map[complement], i]
index_map[num] = i
return None
nums = [2, 7, 11, 15]
target = 9
two_sum = TwoSum(nums, target)
indices = two_sum.find_indices()
print(indices)
OUTPUT –
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
c) Write a Python class named Rectangle constructed by a length and width
and a method which will compute the area of a rectangle.
PROGRAM CODE –
class Rectangle:
def init (self, length, width):
[Link] = length
[Link] = width
def area(self):
return [Link] * [Link]
rectangle = Rectangle(5, 7)
area = [Link]()
print("Area of the rectangle is:", area)
OUTPUT –
d) Write a Python class named Circle constructed by a radius and two
methods which will compute the area and the perimeter of a circle.
PROGRAM CODE –
class Circle:
def init (self, radius):
[Link] = radius
def area(self):
return 3.14 * [Link] ** 2
def perimeter(self):
return 2 * 3.14 * [Link]
circle = Circle(5)
area = [Link]()
perimeter = [Link]()
print("Area of the circle is:", area)
print("Perimeter of the circle is:", perimeter)
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
OUTPUT –
e) Write a Python program to create two empty classes,
Student and Marks. Now create some instances and
check whether they are instances of the said classes or
not. Also, check whether the said classes are subclasses of
the built-in object class or not.
PROGRAM CODE –
class
Student:
pass
class
Marks:
pass
student1 =
Student()student2
= Student() marks1
= Marks() marks2
= Marks()
print(isinstance(student1,
Student))
print(isinstance(student2,
Student))
print(isinstance(marks1,
Marks))
print(isinstance(marks2,
Marks))
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
print(issubclass(Student, object))
print(issubclass(Marks, object))
OUTPUT –