Python3.2
Python3.2
WORKSHEET 3.2
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.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)
OUTPUT:
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
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: "))
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:
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
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))
print(issubclass(Student, object))
print(issubclass(Marks, object))
OUTPUT: