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

Adi 3.2 - Removed

The document contains code snippets demonstrating object-oriented programming concepts in Python including classes, inheritance, polymorphism, and instances. It defines classes like Student, TwoSum, Rectangle, Circle, and checks if objects are instances of those classes. Methods like get_input(), find_indices(), area(), and perimeter() are defined. Everything inherits from the built-in object class. Sample inputs and outputs are included.

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

Adi 3.2 - Removed

The document contains code snippets demonstrating object-oriented programming concepts in Python including classes, inheritance, polymorphism, and instances. It defines classes like Student, TwoSum, Rectangle, Circle, and checks if objects are instances of those classes. Methods like get_input(), find_indices(), area(), and perimeter() are defined. Everything inherits from the built-in object class. Sample inputs and outputs are included.

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/ 4

DEPARTMENT OF

COMPUTER SCIENCE & ENGINEERING

WORKSHEET 3.2

Student Name: Aman Goyal UID: 21BCS3211


Branch: CSE Section/Group: 610-A
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("3237", "Aditya")


student.add_class("4th Sem")
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
student.display_attributes()
print("After removing student_name attribute:")
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 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: "))
def area(self):
return 3.14 * self.radius**2
def perimeter(self):
return 2 * 3.14 * self.radius

circle = Circle()
circle.get_input()
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
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))
print(issubclass(Student, object))
print(issubclass(Marks, object))

Output

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy