Python Oop Assignment Result.pdf 20250310 095423 0000
Python Oop Assignment Result.pdf 20250310 095423 0000
class Employee:
self.PerformanceRating =
PerformanceRating self.DateOfJoining =
DateOfJoining
def get_details(self):
month, day)
def _str_(self):
2. Create five separate variables Bus, Car, Train, Truck and Ship that call the
Fare function.
3. Use a third variable TotalFare to store the sum of fare for each Vehicle Type.
4. Print the TotalFare.
Ans. class
Vehicle:
return fare
Bus = Vehicle()
Car = Vehicle()
Train = Vehicle()
Truck = Vehicle()
Ship = Vehicle()
BusFare = Bus.Fare(100)
CarFare = Car.Fare(70)
TrainFare = Train.Fare(110)
ShipFare = Ship.Fare(115)
Q3. Consider an ongoing test cricket series. Following are the names of the
players and their scores in the test1 and 2.
Test Match 1 : Dhoni : 56 , Balaji : 94
Ans. def
Max_Score(M):
top_player = ""
top_score = 0
top_score = total_score
top_player = player
scores = {'test1': {'Dhoni': 56, 'Balaji': 85}, 'test2': {'Dhoni': 87, 'Balaji': 200}}
'9': 9, '10': 10, 'J': 11, 'Q': 12, 'K': 13, 'A': 14}
deck = list(card_values.keys()) * 4
random.shuffle(deck)
score = 0
current_card = deck.pop(0)
for i in range(7):
print("Invalid input. Please enter 'h' for higher or 'l' for lower.")
continue
next_card = deck.pop(0) print(f"The
result = 'higher'
result = 'lower'
else:
result = 'same'
if result == 'same':
same.") score -= 15
elif (result == 'higher' and guess == 'h') or (result == 'lower' and guess == 'l'):
print("Correct!")
score += 20
else:
print("Incorrect!")
score -= 15
current_card = next_card
c) Now if the speed is Fast the coordinates of the X_pos gets incremented by
22. Print the modified dictionary.
Ans. Car_0 = {} Car_0 = {'color': 'red', 'speed': 'medium', 'X_position': 10,
'Y_position': 72}
if Car_0['speed'] == 'slow':
Car_0['X_position'] += 2
Car_0['X_position'] += 9
Car_0['X_position'] += 22
print(Car_0)
The Output we get: {'color': 'red', 'speed': 'medium', 'X_position': 10, 'Y_position': 72}
@abstractmethod def
relationship(self):
pass
class Son(Parent):
def relationship(self):
print("he is my son")
class Daughter(Parent):
def relationship(self):
print("she is my daughter")
son.relationship()
daughter.relationship()
he is my son
She is my daughter
Q7. Create a program in python to demonstrate Polymorphism. 1. Make use of private
and protected members using python name mangling techniques.
Ans.
class Car:
def _init_(self):
self.__engine = 'V12'
self.__topspeed = "489kmph"
def _protected_method(self):
def display_info(self):
print(f"Engine: {self.__engine}")
print(f"Name: {self._Name}")
print(f"topspeed : {self.__topspeed}")
obj = Car()
obj.display_info()
print(f" {obj.Car_engine}")
print(f"{obj.Car_topspeed}")
Konisegg Egera
topspeed : 489kmph
The Mangling Outputs: V12 489kmph.
Q8. Given a list of 50 natural numbers from 1-50. Create a function that will take every
element from the list and return the square of each element. Use the python map and
filter methods to implement the function on the given list.
Ans. def square_elements(data):
return list(squared_data)
squared_numbers = square_elements(numbers)
print(squared_numbers)
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225, 256, 289, 324, 361, 400, 441, 484, 529, 576,
625, 676, 729, 784, 841, 900, 961, 1024, 1089, 1156, 1225, 1296, 1369, 1444, 1521, 1600, 1681, 1764,
1849, 1936, 2025, 2116, 2209, 2304, 2401, 2500]
Q9. Create a class, Triangle. Its init() method should take self, angle1, angle2, and
angle3 as arguments.
Ans class triangle: def _init_(self,
self.angle1 = angle1
self.angle2 = angle2
self.angle3 = angle3
Lets Take an example: triangle1 = triangle(60,60,60) triangle2 =
angles: 60 60 60
triangle:
, angle2 , angle3):
self.angle1 = angle1
self.angle2 = angle2
self.angle3 = angle3
print(triangle.number_of_sides)
my_triangle = triangle(60,60,60)
Q11. Create a method named check_angles. The sum of a triangle's three angles should
return True if the sum is equal to 180, and False otherwise. The method should print
whether the angles belong to a triangle or not.
Ans.
class Triangle:
number_of_sides = 3
self.angle1 = angle1
self.angle2 = angle2
self.angle3 = angle3
def check_angles(self):
else:
Ans. class
Triangle:
number_of_sides = 3
def _init_(self, angle1, angle2, angle3):
self.angle1 = angle1
self.angle2 = angle2
self.angle3 = angle3
def check_angles(self):
else:
my_triangle = Triangle(120,60,45)
result = my_triangle.check_angles()
print(result)
my_triangle = Triangle(30,60,45)
result = my_triangle.check_angles()
print(result)
False
The Triangle is an acute Triangle True.
11.2 Create an instance of the triangle class and call all the defined methods.
Ans. class Triangle:
number_of_sides = 3
self.angle1 = angle1
self.angle2 = angle2
self.angle3 = angle3
def check_angles(self):
else:
result = my_triangle.check_angles()
print(result)
False.
number_of_sides = 3
self.angle1 = angle1
self.angle2 = angle2
self.angle3 = angle3
def check_angles(self):
return True
else:
return False
class isosceles_triangle(Triangle):
super()._init_(angle1,angle2,angle2)
class equilateral_triangle(Triangle):
super()._init_(60,60,60) print("This is
an Equilateral Triangle")
print(iso_triangle.check_angles())
print(right_triangle.check_angles())
equilateral_triangle = equilateral_triangle()
print(equilateral_triangle.check_angles())
is an isosceles Triangle
True
This is an right
Triangle True
True.
number_of_sides = 3
self.angle1 = angle1
self.angle2 = angle2
self.angle3 = angle3
def check_angles(self):
return True
else:
return False
class isosceles_triangle(Triangle):
class right_triangle(Triangle):
def is_right(self):
class equilateral_triangle(Triangle):
def _init_(self):
is Equilateral Triangle")
def is_equilateral(self):
print(iso_triangle.check_angles())
right_triangle = right_triangle(30, 60)
print(right_triangle.check_angles())
equilateral_triangle = equilateral_triangle()
print(equilateral_triangle.check_angles())
True
True
True
Q12. Create a class isosceles_right_triangle which inherits from
isosceles_triangle and right_triangle.
Ans. class
Triangle:
number_of_sides = 3
self.angle1 = angle1
self.angle2 = angle2
self.angle3 = angle3
def check_angles(self):
return True
else:
return False
class isosceles_triangle(Triangle):
super()._init_(angle1, angle2)
def is_isosceles(self):
class right_triangle(Triangle):
def is_right(self):
result = iso_triangle.check_angles()
print(result)
iso_triangle = isosceles_right_triangle(45)
result = iso_triangle.check_angles()
print(result)
super()._init_(angle, 45)
def is_isosceles_right(self):