Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

Name – Jai Ramteke

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)

r = int (input("Enter the roll number: "))


name = input("Enter the name: ")
m1 = int (input("Enter the marks of Science: "))
m2 = int (input("Enter the marks of Maths: "))
m3 = int (input("Enter the marks of History: "))
s1 = Student()
s1.getData(r, name, m1, m2, m3)
s1.displayData()

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:

# Python3 program to Add two complex numbers

# User Defined Complex class


class Complex:

# Constructor to accept
# real and imaginary part
def __init__(self, tempReal, tempImaginary):
self.real = tempReal;
self.imaginary = tempImaginary;

# Defining addComp() method


# for adding two complex number
def addComp(self, C1, C2):

# creating temporary variable


temp=Complex(0, 0)

# adding real part of complex numbers


temp.real = C1.real + C2.real;

# adding Imaginary part of complex numbers


temp.imaginary = C1.imaginary + C2.imaginary;

# returning the sum


return temp;

# Driver code
if __name__=='__main__':
# First Complex number
C1 = Complex(3, 2);

# printing first complex number


print("Complex number 1 :", C1.real, "+ i" + str(C1.imaginary))

# Second Complex number


C2 = Complex(9, 5);

# printing second complex number


print("Complex number 2 :", C2.real, "+ i" + str(C2.imaginary))

# for Storing the sum


C3 = Complex(0, 0)

# calling addComp() method


C3 = C3.addComp(C1, C2);

# printing the sum


print("Sum of complex number :", C3.real, "+ i"+ str(C3.imaginary))

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

a=int(input("Enter length of rectangle: "))


b=int(input("Enter breadth of rectangle: "))
c=int(input("Enter height of rectangle: "))
obj=rectangle(a,b,c)
print("Area of rectangle:",obj.area())
print("Volume of rectangle:",obj.vol())
print()

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:

# Python3 program to check if


# a point lies inside a circle
# or not

def pt_in_circle(circle_x, circle_y, rad, x, y):

# Compare radius of circle


# with distance of its center
# from given point
if ((x - circle_x) * (x - circle_x) +
(y - circle_y) * (y - circle_y) <= rad * rad):
return True;
else:
return False;

# 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:

def add(self, a, n):


self.c = a*n
return self.c

# creating an object of class

# using same add method by passing strings


# as argument
a=input("Enter a string")
n=int(input("Enter how many times you want to repeat the string: "))
obj = operatoroverloading()
result = obj.add(a,n)
print("Concatenated string is", result)

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:

# Python program to print current date and week day

from datetime import date


import datetime
import calendar

# calling the today


# function of date class
today = date.today()

print("Today's date is", today)

curr_date = date.today()
print(calendar.day_name[curr_date.weekday()])

You might also like