PYTHON OOPs ASSIGNMENT
NAME HARINI S V
EMAIL harini102405@[Link]
ASSIGNMENT QUESTIONS :
Q1. Write a Python program to demonstrate multiple inheritance.
1. Employee class has 3 data members:
o EmployeeID (String)
o Gender (String)
o Salary and PerformanceRating (out of 5) of type int.
It has a get() function to get these details from the user.
2. JoiningDetail class has a data member DateOfJoining of type Date and a
function getDoJ() to get the date of joining of employees.
3. Information class uses the marks from Employee class and the
DateOfJoining date from JoiningDetail class to calculate the top 3
employees based on their ratings and then display, using readData, all
the details on these employees in ascending order of their Date of
Joining.
● Employee stores basic info and has get() for initialization.
● JoiningDetail stores date_of_joining (as [Link]).
● Information inherits both and combines data to compute top 3 by
PerformanceRating, then sorts those three by date_of_joining ascending and prints
details.
Q2. Write a Python program to demonstrate Polymorphism.
1. Class Vehicle with a parameterized function Fare, that takes input value
as fare and returns it to calling Objects.
2. Create five separate variables: Bus, Car, Train, Truck, 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.
● Polymorphism here is, demonstrated by different instances of the same class or
subclasses using the same method fare -- they return their respective fares.
● It shows polymorphism by calling the same method on different objects.
Q3. Consider an ongoing test cricket series. Following are the names of the
players and their scores in test1 and 2.
Test Match 1:
Dhoni : 56 , Balaji : 94
Test Match 2:
Balaji : 80 , Dravid : 105
Calculate the highest number of runs scored by an individual cricketer in both of the
matches.
Create a Python function Max_Score(M) that reads a dictionary M that recognizes the
player with the highest total [Link] function will return (Top player, Total Score).You
can consider the Top player as String who is the highest scorer and Top score as Integer.
Input : Max_Score({'test1': {'Dhoni':56, 'Balaji':85}, 'test2': {'Dhoni':87, 'Balaji':200}})
Output : ('Balaji', 200)
● It aggregate scores across matches (sum per player), then find the player with the
maximum total.
Q4. Create a simple Card game in which there are 8 cards randomly chosen
from a deck.
The first card is shown face up. The game asks the player to predict whether the
next card in the selection will have a higher or lower value than the currently
showing card.
For example, say the card that’s shown is a 3. The player chooses “higher,” and the next
card is shown.
If that card has a higher value, the player is correct.
In this example, if the player had chosen “lower,” they would have been [Link] the
player guesses correctly, they get 20 [Link] they choose incorrectly, they lose 15 [Link]
the next card to be turned over has the same value as the previous card, the player is
incorrect.
● We represent cards by ranks 1..13 (Ace=1 .. King=13).
● Use [Link] from a standard 52-card deck .
● Play through the chosen 8 cards, prompting the user for 'higher' or 'lower' and
update score.
Q5. Create an empty dictionary called Car_0.
Then fill the dictionary with keys: color, speed, x_position, and y_position.
car_0 = {'x_position': 10, 'y_position': 72, 'speed': 'medium'}
a) If the speed is slow, the coordinates of x_pos get incremented by 2.
b) If the speed is medium, the coordinates of x_pos get incremented by 9.
c) If the speed is fast, the coordinates of x_pos get incremented by 22.
Print the modified dictionary.
● Simple dictionary manipulation with conditional logic.
● It shows both case insensitive handling and a small function
Q6. Show a basic implementation of abstraction in Python using abstract
classes.
1. Create an abstract class in Python.
2. Implement abstraction with the base and derived classes as abstract class.
● Creates an abstract base class (AbstractVehicle) with an abstract method
start_engine.
● Concrete classes must implement that method.
Q7. Create a program in Python to demonstrate Polymorphism.
1. Make use of private and protected members using name mangling techniques.
● Protected members : single underscore __value – by convention accessible but treated as
internal.
● Private members : double underscore __secret -- Python name-mangles to
_ClassName__secret.
● Two classes implement a method describe() that access _value (protected) and their own
__secret. This shows different classes with a common interface (polymorphism) and name-
mangled private attributes.
Q8. Given a list of numbers between 1–50. Create a function to remove every
10th element from the list and form a square for each element.
Use map and filter methods to implement the logic.
● map applies a function to every item(square).
● Filter can be used to, for example, include only even numbers before squaring – I’ll
show both full-squares and example using filter to square only odd or even elements
per demonstration
Q9. Create a class Triangle. Its __init__() method should take self, angle1, angle2,
and angle3 as arguments.
● __init__() is the constructor method.
● It runs automatically when you create an object.
● self refers to the current object.
● angle1, angle2 and angle3 are stored as instance variables.
Q10. Create a class variable named number_of_sides and set it equal to 3.
Class Variable
● Declared inside the class, but outside all methods.
● It is shared by all objects of the class.
● It represents a property that is common to all triangles (every triangle has 3 sides).
Instance Variable
● These belong to a specific object (instance).
● Each triangle can have different angles.
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.
11.1 Write methods to verify if the triangle is an acute triangle or obtuse triangle.
11.2 Create an instance of the triangle class and call all the defined methods.
11.3 Create three child classes of triangle class:
● Isosceles_Triangle
● Right_Triangle
● Equilateral_Triangle
11.4 Define methods which check for their properties.
Class Variable
● All triangles share this — it’s common for every triangle object.
Constructor
● Stores each triangle’s three angles as instance variables.
Q12. Create a class Isosceles_Right_Triangle which inherits from
Isosceles_Triangle and Right_Triangle.
12.1 Define methods which check for their properties.
Multiple Inheritance
● The new class inherits from two parent classes:
o Isosceles_Triangle → for checking if two angles are equal.
o Right_Triangle → for checking if one angle is 90°.
● This demonstrates multiple inheritance in Python.
Method: check_isosceles_right()
This method checks both conditions together:
1. The angles form a valid triangle (sum = 180).
2. One angle is 90°.
3. Two angles are equal.
If all are true → It is an Isosceles Right Triangle.