Professional Documents
Culture Documents
Name - Jai Ramteke Class - TYBBA (CA) Roll No - 50 Python Assignment 5 Set A
Name - Jai Ramteke Class - TYBBA (CA) Roll No - 50 Python Assignment 5 Set A
Class – TYBBA(CA)
Roll No - 50
Python Assignment 5
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.
Code:
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: ", self.average())
def total(self):
return (Student.marks[0] + Student.marks[1] +Student.marks[2])
def average(self):
return ((Student.marks[0] + Student.marks[1] +Student.marks[2])/3)
2) Write a Python program that defines a class named circle with attributes radius
and center, where center is a point object and radius is number. Accept center and
radius from user. Instantiate a circle object that represents a circle with its center
and radius as accepted input.
Code:
class Circle:
def __init__(self,r,x,y):
self.radius = r
self.x=x
self.y=y
def get_radius(self):
print(self.radius)
def get_center(self):
print("The center of the circle is["+str(self.x)+","+str(self.y)+"]")
x=int(input("Enter the x coordinate "))
y=int(input("Enter the y coordinate "))
radius=int(input("Enter radius "))
NewCircle = Circle(radius,x,y)
NewCircle.get_radius()
NewCircle.get_center()
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.
Code:
class IOString():
def __init__(self):
self.str1 = ""
def get_String(self):
self.str1 = input()
def print_String(self):
print("The string in upper case is"+self.str1.upper())
def reverse(self):
self.str1=self.str1[::-1]
print("the reversed string in lower case is:"+self.str1.lower())
str1 = IOString()
str1.get_String()
str1.print_String()
str1.reverse()
4) Write Python class to perform addition of two complex numbers using binary +
operator overloading.
Code:
# Constructor to accept
# real and imaginary part
def __init__(self, tempReal, tempImaginary):
self.real = tempReal;
self.imaginary = tempImaginary;
# Driver code
if __name__=='__main__':
# First Complex number
C1 = Complex(3, 2);
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.
Code:
class rectangle():
def __init__(self,breadth,length,height):
self.breadth=breadth
self.length=length
self.height=height
def area(self):
return self.breadth*self.length
def vol(self):
return self.breadth*self.length*self.height
2) Write a function named pt_in_circle that takes a circle and a point and returns
true if point lies on the boundary of circle.
Code:
# Driver Code
x = 1;
y = 1;
circle_x = 0;
circle_y = 1;
rad = 2;
if(pt_in_circle(circle_x, circle_y, rad, x, y)):
print("Inside");
else:
print("Outside");
3) Write a Python Program to Create a Class Set and Get All Possible Subsets
from a Set of Distinct Integers.
Code:
class sub:
def f1(self, s1):
return self.f2([], sorted(s1))
def f2(self, curr, s1):
if s1:
return self.f2(curr, s1[1:]) + self.f2(curr + [s1[0]], s1[1:])
return [curr]
a=[]
n=int(input("Enter number of elements of list: "))
for i in range(0,n):
b=int(input("Enter element: "))
a.append(b)
print("Subsets: ")
print(sub().f1(a))
4) Write a python class to accept a string and number n from user and display n
repetition of strings using by overloading * operator.
Code:
class operatoroverloading:
Set C:
1) Python Program to Create a Class which Performs Basic Calculator Operations.
Code:
class cal():
def __init__(self,a,b):
self.a=a
self.b=b
def add(self):
return self.a+self.b
def mul(self):
return self.a*self.b
def div(self):
return self.a/self.b
def sub(self):
return self.a-self.b
a=int(input("Enter first number: "))
b=int(input("Enter second number: "))
obj=cal(a,b)
choice=1
while choice!=0:
print("0. Exit")
print("1. Add")
print("2. Subtraction")
print("3. Multiplication")
print("4. Division")
choice=int(input("Enter choice: "))
if choice==1:
print("Result: ",obj.add())
elif choice==2:
print("Result: ",obj.sub())
elif choice==3:
print("Result: ",obj.mul())
elif choice==4:
print("Result: ",round(obj.div(),2))
elif choice==0:
print("Exiting!")
else:
print("Invalid choice!!")
print()
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.
Code:
curr_date = date.today()
print(calendar.day_name[curr_date.weekday()])