Assignment 5 Python
Assignment 5 Python
Assignment 5 Python
Set A
1. Write a Python Program to Accept, Delete and Display students
details such as Roll.No, Name, Marks in three subject, using
Classes. Also display percentage of each student.
Ans :-
class Student:
marks = []
def getData(self, rn, name, m1, m2, m3):
Student.rn = rn
Student.name = name
Student.marks.append(m1)
Student.marks.append(m2)
Student.marks.append(m3)
def displayData(self):
print ("Roll Number is: ", Student.rn)
print ("Name is: ", Student.name)
#print ("Marks in subject 1: ", Student.marks[0])
#print ("Marks in subject 2: ", Student.marks[1])
#print ("Marks in subject 3: ", Student.marks[2])
print ("Marks are: ", Student.marks)
print ("Total Marks are: ", self.total())
print ("Percentage are: ", self.percent())
def total(self):
return (Student.marks[0] + Student.marks[1] +Student.marks[2])
def percent(self):
return ((Student.marks[0] + Student.marks[1] +Student.marks[2])/300)*100
def delete(self):
return (obj.remove(n))
s1 = Student()
s1.getData(r, name, m1, m2, m3)
s1.displayData()
Output:
3.Write a Python class which has two methods get_String and
print_String. get_String accept a string from the user and
print_String print the string in upper case. Further modify the
program to reverse a string word by word and print it in lower
case
Ans:-
class IOString():
def __init__(self):
self.str1 = ""
def get_String(self):
self.str1 = input()
def print_String(self):
print(self.str1.upper())
str1 = IOString()
str1.get_String()
str1.print_String()
class rev:
string=''
def get_string(self):
self.string=input("Enter a string :")
def print_string(self):
words=self.string.split(' ')
rev_words=''.join(reversed(words))
print(rev_words.lower())
obj=rev()
obj.get_string()
obj.print_string()def main():
box = Rectangle()
box.width = 100.0
box.height = 200.0
box.corner = Point()
box.corner.x = 50.0
box.corner.y = 50.0
print(box.corner.x)
print(box.corner.y)
circle = Circle
circle.center = Point()
circle.center.x = 150.0
circle.center.y = 100.0
circle.radius = 75.0
print(circle.center.x)
print(circle.center.y)
print(circle.radius)
print(point_in_circle(box.corner, circle))
print(rect_in_circle(box, circle))
print(rect_circle_overlap(box, circle))
if __name__ == '__main__':
main()
Output:-
4. Write Python class to perform addition of two complex numbers using
binary + operator overloading.
Ans:-
print("Format for writing complex number: a+bj.\n")
c1 = complex(input("Enter First Complex Number: "))
c2 = complex(input("Enter second Complex Number: "))
print("Sum of both the Complex number is", c1 + c2)
Output:-
Set B
1. Define a class named Rectangle which can be constructed by
a length and width. The Rectangle class has a method which
can compute the area and volume.
Ans :-
class Rectangle():
def __init__(self,l,w):
self.length = l
self.width = w
def area(self):
return self.length*self.width
rect = Rectangle(8,6)
print(rect.area())
Output:-
2.Write a function named pt_in_circle that takes a circle and a point and
returns true if point lies on the boundry of circle.
Ans:-
def isInside(circle_x, circle_y, rad, x, y):
# Driver Code
x = 2;
y = 2;
circle_x = 0;
circle_y = 1;
rad = 2;
if(isInside(circle_x, circle_y, rad, x, y)):
print("False");
else:
print("True");
Output:-
3.Write a Python Program to Create a Class Set and Get All Possible
Subsets from a Set of Distinct Integers.
Ans:-
class py_solution:
def sub_sets(self, sset):
return self.subsetsRecur([], sorted(sset))
print(py_solution().sub_sets([4,5,6]))
Output:-
4. Write a python class to accept a string and number n from user and
display n repetition of strings using by overloading * operator.
Ans:-
s = "asdhdtg"
n = 10
print(s * n)
Output:-
Set C
1. Python Program to Create a Class which Performs Basic Calculator
Operations.
Ans:-
class Calculator:
def add(self, a, b):
return a+b
while True:
print("1: Add")
print("2: Subtract")
print("3: Multiply")
print("4: Divide")
print("5: Exit")
#If not then ask fo the input and call appropiate methods
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
if(ch == 1):
print(a, "+", b, "=", my_cl.add(a, b))
elif(ch == 2):
print(a, "-", b, "=", my_cl.subtract(a, b))
elif(ch == 3):
print(a, "*", b, "=", my_cl.multiply(a, b))
elif(ch == 4):
print(a, "/", b, "=", my_cl.divide(a, b))
else:
print("Invalid Input")
Output:-
2. Define datetime module that provides time object. Using this
module write a program that gets current date and time and print
day of the week.
Ans:-
from datetime import datetime
Output:-