Docsity

Python Lab Assignment, Assignments of Applications of Computer Sciences

Toggle sidebar
bg1
Assignment 5
5.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.
class Student:
def __init__(self, roll_no, name, marks1, marks2, marks3):
self.roll_no = roll_no
self.name = name
self.marks = [marks1, marks2, marks3]
def calculate_percentage(self):
total_marks = sum(self.marks)
percentage = (total_marks / 300) * 100
return percentage
def display(self):
percentage = self.calculate_percentage()
print(f"Roll No: {self.roll_no}")
print(f"Name: {self.name}")
print(f"Marks: {self.marks[0]}, {self.marks[1]}, {self.marks[2]}")
print(f"Percentage: {percentage:.2f}%")
class StudentDatabase:
def __init__(self):
self.students = {}
def add_student(self, roll_no, name, marks1, marks2, marks3):
if roll_no in self.students:
print("Student with this Roll No already exists.")
else:
self.students[roll_no] = Student(roll_no, name, marks1, marks2, marks3)
print("Student added successfully.")
pf3
pf4
pf5
pf8
pf9

Partial preview of the text

Download Python Lab Assignment and more Assignments Applications of Computer Sciences in PDF only on Docsity!

Assignment 5

5.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.

class Student: def init(self, roll_no, name, marks1, marks2, marks3): self.roll_no = roll_no self.name = name self.marks = [marks1, marks2, marks3] def calculate_percentage(self): total_marks = sum(self.marks) percentage = (total_marks / 300) * 100 return percentage def display(self): percentage = self.calculate_percentage() print(f"Roll No: {self.roll_no}") print(f"Name: {self.name}") print(f"Marks: {self.marks[0]}, {self.marks[1]}, {self.marks[2]}") print(f"Percentage: {percentage:.2f}%") class StudentDatabase: def init(self): self.students = {} def add_student(self, roll_no, name, marks1, marks2, marks3): if roll_no in self.students: print("Student with this Roll No already exists.") else: self.students[roll_no] = Student(roll_no, name, marks1, marks2, marks3) print("Student added successfully.")

def delete_student(self, roll_no): if roll_no in self.students: del self.students[roll_no] print("Student deleted successfully.") else: print("Student with this Roll No does not exist.") def display_all_students(self): if not self.students: print("No students to display.") for student in self.students.values(): student.display() print("-" * 30)

Function to handle user inputs

def main(): db = StudentDatabase() while True: print("\nStudent Database Menu") print("1. Add Student") print("2. Delete Student") print("3. Display All Students") print("4. Exit") choice = input("Enter your choice: ") if choice == '1': roll_no = input("Enter Roll No: ") name = input("Enter Name: ") marks1 = float(input("Enter Marks in Subject 1: "))

class Circle: def init(self, center, radius): self.center = center self.radius = radius def str(self): return f"Circle with center at {self.center} and radius {self.radius}" def main():

Accept user input for the center and radius

x = float(input("Enter the x-coordinate of the center: ")) y = float(input("Enter the y-coordinate of the center: ")) radius = float(input("Enter the radius of the circle: "))

Create a Point object for the center

center = Point(x, y)

Create a Circle object

circle = Circle(center, radius)

Display the Circle object

print(circle) if name == "main": main()

5.A.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.

class MyClass:

def Get(self): self.MyStr=input("Enter any String:") def Display(self): s=self.MyStr print("Upper Case:", s.upper()) cnt=len(s) i=cnt- RevStr="" while(i>=0): RevStr=RevStr+s[i] i=i- print("Reverse & Lower case:",RevStr.lower()) a=MyClass() a.Get() a.Display()

5.A.4) Write Python class to perform addition of two complex numbers using binary +

operator overloading.

class Complex(): def initComplex(self): self.realPart = int(input("Enter the Real Part: ")) self.imgPart = int(input("Enter the Imaginary Part: ")) def display(self): print(self.realPart,"+",self.imgPart,"i", sep="") def sum(self, c1, c2): self.realPart = c1.realPart + c2.realPart

def Perimeter(self): return 2*(self.length+self.width) l=int(input("Enter length of Rectangle:")) w=int(input("Enter width of Rectangle:")) obj= Rectangle(l,w) print("Area of Rectangle:", round(obj.area(),2)) print("Area of Rectangle:", round(obj.Perimeter(),2))

5.B.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.

class Point: def init(self, center=(0,0)): self.x = center[0] self.y = center[1] def print_point(self): print("The point is at ({x}, {y})".format(x=self.x, y=self.y)) class Circle: def init(self, center, radius): self.center = Point(center) self.radius = radius def print_circle(self): print("The circle's center is at ({x}, {y}) and a radius of {radius}" .format(x=self.center.x, y=self.center.y, radius=self.radius)) def point_in_circle(c, p): if ((p.x <= c.center.x+c.radius and p.x >= c.center.x-c.radius) and (p.y <= c.center.y+c.radius and p.y >= c.center.y-c.radius)): return True else: return False

5.B.3) Write a Python Program to Create a Class Set and Get All Possible Subsets

from a Set of Distinct Integers.

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

5.B.4) Write a python class to accept a string and number n from user and display n

repetition of strings using by overloading * operator.

class StringRepeater: def init(self,input_string): self.input_string = input_string def mul(self, n): if isinstance(n, int): return self.input_string*n else: raise ValueError("The repetition factor should be an integer") #Get input from the user user_input_string = input("Enter a string:") repetition_factor =int(input("Enter the repetition factor (an integer):"))