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

Sahil Raj - 21BCS3383 Python 3.2

This document contains a worksheet for a student named Sahil Raj for a 4th semester Programming in Python course. The worksheet contains 5 programming problems to implement object-oriented programming concepts in Python like classes, inheritance, polymorphism, and more. For each problem, the student has provided the Python code to solve it along with the output.

Uploaded by

Warriors Neo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views

Sahil Raj - 21BCS3383 Python 3.2

This document contains a worksheet for a student named Sahil Raj for a 4th semester Programming in Python course. The worksheet contains 5 programming problems to implement object-oriented programming concepts in Python like classes, inheritance, polymorphism, and more. For each problem, the student has provided the Python code to solve it along with the output.

Uploaded by

Warriors Neo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

DEPARTMENT OF

COMPUTER SCIENCE & ENGINEERING

WORKSHEET 3.2

Student Name: Sahil Raj UID: 21BCS3383


Branch: CSE Section/Group: 610-B
Semester: 4th Date of Performance: 05/05/23
Subject Name: Programming in Python Subject Code: 21CSP-259

1. Aim: Program to implement concept of oops such as classes, inheritance


and polymorphism.

2. Source Code:
1. 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.
class Student:
def __init__(self, student_id, student_name):
self.student_id = student_id
self.student_name = student_name
def add_class(self, student_class):
self.student_class = student_class
def display_attributes(self):
attributes = vars(self)
print("Attributes and their values:")
for attr, value in attributes.items():
print(attr, ":", value)
def remove_name(self):
del self.student_name

student = Student("3383", "Sahil")


student.add_class("4th Sem")
student.display_attributes()
print("After removing student_name attribute:")
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

student.remove_name()
student.display_attributes()

OUTPUT:

2. 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.
class TwoSum:
def __init__(self):
self.nums = []
self.target = 0
def get_input(self):
self.nums = list(map(int, input("Enter a list of numbers: ").split()))
self.target = int(input("Enter the target number: "))
def find_indices(self):
indices = {}
for i, num in enumerate(self.nums):
if self.target - num in indices:
return [indices[self.target - num], i]
indices[num] = i

twosum = TwoSum()
twosum.get_input()
indices = twosum.find_indices()
print(indices)
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

OUTPUT:

3. Write a Python class named Rectangle constructed by a length and width and
a method which will compute the area of a rectangle.
class Rectangle:
def __init__(self):
self.length = 0
self.width = 0
def get_input(self):
self.length = float(input("Enter the length: "))
self.width = float(input("Enter the width: "))
def area(self):
return self.length * self.width
rectangle = Rectangle()
rectangle.get_input()
print("The area of the rectangle is:", rectangle.area())

OUTPUT:

4. Write a Python class named Circle constructed by a radius and two methods
which will compute the area and the perimeter of a circle.
class Circle:
def __init__(self):
self.radius = 0
def get_input(self):
self.radius = float(input("Enter the radius of the circle: "))
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

def area(self):
return 3.14 * self.radius**2
def perimeter(self):
return 2 * 3.14 * self.radius

circle = Circle()
circle.get_input()
print("The area of the circle is:", circle.area())
print("The perimeter of the circle is:", circle.perimeter())

OUTPUT:

5. 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.
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:

You might also like