Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

Python3.2

This document is a worksheet for a Computer Science and Engineering course focusing on Python programming concepts, specifically object-oriented programming (OOP) including classes, inheritance, and polymorphism. It contains multiple programming exercises with source code examples for creating classes like Student, TwoSum, Rectangle, and Circle, along with their functionalities. Each exercise includes a description of the task, the source code, and expected outputs.

Uploaded by

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

Python3.2

This document is a worksheet for a Computer Science and Engineering course focusing on Python programming concepts, specifically object-oriented programming (OOP) including classes, inheritance, and polymorphism. It contains multiple programming exercises with source code examples for creating classes like Student, TwoSum, Rectangle, and Circle, along with their functionalities. Each exercise includes a description of the task, the source code, and expected outputs.

Uploaded by

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

DEPARTMENT OF

COMPUTER SCIENCE &


ENGINEERING

WORKSHEET 3.2

Student Name: Shivam Shrivastava UID: 22BCS80037


Branch: CSE Section/Group: 911/B
Semester: 4th Date of Performance:
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("80008", "Anjali")


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)

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:

You might also like