Python OOPs Assignment
Python OOPs Assignment
"cells": [
{
"cell_type": "markdown",
"id": "7c66b5f9-d5b6-4295-a1cd-ab59aa9df915",
"metadata": {},
"source": [
"***Q1. Write a Python program to demonstrate multiple inheritance.***\r\n",
"1. Employee class has 3 data members EmployeeID, Gender (String), Salary and\
r\n",
"PerformanceRating(Out of 5) of type int. It has a get() function to get these
details from\r\n",
"the user.\r\n",
"2. JoiningDetail class has a data member DateOfJoining of type Date and a
function\r\n",
"getDoJ to get the Date of joining of employees.\r\n",
"3. Information Class uses the marks from Employee class and the DateOfJoining
date\r\n",
"from the JoiningDetail class to calculate the top 3 Employees based on their
Ratings\r\n",
"and then Display, using readData, all the details on these employees in
Ascending\r\n",
"order of their Date Of Joining."
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "8ac56575-18e4-4630-a7a6-b95d0403a901",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter Employee ID: 2454\n",
"Enter Gender: Male\n",
"Enter Salary: 60000\n",
"Enter Performance Rating (Out of 5): 4\n",
"Enter Date of Joining (YYYY-MM-DD): 2021-09-06\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Employee ID: 2454, Gender: Male, Salary: 60000.0, Performance Rating: 4,
Date of Joining: 2021-09-06\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter Employee ID: 5565\n",
"Enter Gender: Female\n",
"Enter Salary: 50000\n",
"Enter Performance Rating (Out of 5): 3\n",
"Enter Date of Joining (YYYY-MM-DD): 2014-08-15\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Employee ID: 5565, Gender: Female, Salary: 50000.0, Performance Rating: 3,
Date of Joining: 2014-08-15\n",
"Employee ID: 2454, Gender: Male, Salary: 60000.0, Performance Rating: 4,
Date of Joining: 2021-09-06\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter Employee ID: 7877\n",
"Enter Gender: Male\n",
"Enter Salary: 45000\n",
"Enter Performance Rating (Out of 5): 4\n",
"Enter Date of Joining (YYYY-MM-DD): 2021-08-15\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Employee ID: 5565, Gender: Female, Salary: 50000.0, Performance Rating: 3,
Date of Joining: 2014-08-15\n",
"Employee ID: 7877, Gender: Male, Salary: 45000.0, Performance Rating: 4,
Date of Joining: 2021-08-15\n",
"Employee ID: 2454, Gender: Male, Salary: 60000.0, Performance Rating: 4,
Date of Joining: 2021-09-06\n"
]
}
],
"source": [
"class Employee:\n",
" def get(self):\n",
" self.EmployeeID = int(input(\"Enter Employee ID: \"))\n",
" self.Gender = input(\"Enter Gender: \")\n",
" self.Salary = float(input(\"Enter Salary: \"))\n",
" self.PerformanceRating = int(input(\"Enter Performance Rating (Out of
5): \"))\n",
"\n",
"class JoiningDetail:\n",
" def getDoJ(self):\n",
" self.DateOfJoining = input(\"Enter Date of Joining (YYYY-MM-DD): \")\
n",
"\n",
"class Information(Employee, JoiningDetail):\n",
" def readData(self):\n",
" employees = []\n",
" for _ in range(3):\n",
" emp = Information()\n",
" emp.get()\n",
" emp.getDoJ()\n",
" employees.append(emp)\n",
" top_3_employees = sorted(employees, key=lambda x:
x.PerformanceRating, reverse=True)[:3]\n",
" top_3_employees = sorted(top_3_employees, key=lambda x:
x.DateOfJoining)\n",
" \n",
"\n",
" for emp in top_3_employees:\n",
" print(f\"Employee ID: {emp.EmployeeID}, Gender: {emp.Gender},
Salary: {emp.Salary}, Performance Rating: {emp.PerformanceRating}, Date of Joining:
{emp.DateOfJoining}\")\n",
"\n",
"# Example usage\n",
"info = Information()\n",
"info.readData()"
]
},
{
"cell_type": "markdown",
"id": "1b244a2b-4d2f-46d2-a4d9-c676209881f6",
"metadata": {},
"source": [
"***###############################################################################
#######################################***\n"
]
},
{
"cell_type": "markdown",
"id": "77501244-8848-442c-b0db-7b4d24cdf5d4",
"metadata": {},
"source": [
"***Q.2 Write a Python program to demonstrate Polymorphism.***\r\n",
"1. Class Vehicle with a parameterized function Fare, that takes input value as
fare and\r\n",
"returns it to calling Objects.\r\n",
"2. Create five separate variables Bus, Car, Train, Truck and Ship that call
the Fare\r\n",
"function.\r\n",
"3. Use a third variable TotalFare to store the sum of fare for each Vehicle
Type.\r\n",
"4. Print the TotalFare."
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "67208f71-03db-44e5-bf3d-fceae34f73a1",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"750\n"
]
}
],
"source": [
"class Vehicle:\n",
" def Fare(self, fare):\n",
" return fare\n",
"\n",
"# Creating instances of the Vehicle class\n",
"bus = Vehicle()\n",
"car = Vehicle()\n",
"train = Vehicle()\n",
"truck = Vehicle()\n",
"ship = Vehicle()\n",
"\n",
"# Calling the Fare function for each vehicle and storing the fare\n",
"bus_fare = bus.Fare(50)\n",
"car_fare = car.Fare(100)\n",
"train_fare = train.Fare(150)\n",
"truck_fare = truck.Fare(200)\n",
"ship_fare = ship.Fare(250)\n",
"\n",
"# Calculating the total fare\n",
"total_fare = bus_fare + car_fare + train_fare + truck_fare + ship_fare\n",
"\n",
"# Printing the total fare\n",
"print(total_fare)"
]
},
{
"cell_type": "markdown",
"id": "6bc551ad-2fa0-4f8e-9f91-a6afeb11059a",
"metadata": {},
"source": [
"***###############################################################################
#######################################***"
]
},
{
"cell_type": "markdown",
"id": "3abfa9e2-d127-46fb-a051-98abe86ade9a",
"metadata": {},
"source": [
"***Q3. Consider an ongoing test cricket series. Following are the names of the
players and their\n",
"scores in the test1 and 2.***\n",
"Test Match 1 :\n",
"Dhoni : 56 , Balaji : 94\n",
"Test Match 2 :\n",
"Balaji : 80 , Dravid : 105\n",
"Calculate the highest number of runs scored by an individual cricketer in both
of the matches.\n",
"Create a python function Max_Score (M) that reads a dictionary M that
recognizes the player\n",
"with the highest total score. This function will return ( Top player , Total
Score ) . You can\n",
"consider the Top player as String who is the highest scorer and Top score as
Integer .\n",
"Input : Max_Score({‘test1’:{‘Dhoni’:56, ‘Balaji : 85}, ‘test2’:{‘Dhoni’ 87,
‘Balaji’’:200}})\n",
"Output : (‘Balaji ‘ , 200)"
]
},
{
"cell_type": "markdown",
"id": "9548a6d6-e897-425b-8b3e-957ce79117b0",
"metadata": {},
"source": [
"***###############################################################################
#######################################***"
]
},
{
"cell_type": "code",
"execution_count": 21,
"id": "1f78f8ca-78ba-4205-a072-63789f67a52f",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Total Run Scored by Each Playes in 2 Tests: \n",
"{'Dhoni': 143, 'Balaji': 285}\n",
"Player With highest score\n",
"('Balaji', 285)\n"
]
}
],
"source": [
"M = {\"test1\": {'Dhoni': 56, \"Balaji\": 85}, 'test2': {'Dhoni': 87,
'Balaji': 200}}\n",
"\n",
"def Max_Score(d):\n",
" total = {}\n",
" for k in d.keys():\n",
" for n in d[k].keys():\n",
" if n in total.keys():\n",
" total[n] = total[n] + d[k][n]\n",
" else:\n",
" total[n] = d[k][n]\n",
" print(\"Total Run Scored by Each Playes in 2 Tests: \")\n",
" print(total)\n",
"\n",
" print(\"Player With highest score\")\n",
" maxtotal = -1\n",
" for n in total.keys():\n",
" if total[n] > maxtotal:\n",
" maxname = n\n",
" maxtotal = total[n]\n",
"\n",
" return (maxname, maxtotal)\n",
"\n",
"summary = Max_Score(M)\n",
"print(summary)"
]
},
{
"cell_type": "markdown",
"id": "83089df6-9264-4900-ad3d-fe48aac31a6a",
"metadata": {},
"source": [
"***###############################################################################
#######################################***"
]
},
{
"cell_type": "markdown",
"id": "4ed3e794-9dcd-4d06-aaf9-44560c349667",
"metadata": {},
"source": [
"***Q4. Create a simple Card game in which there are 8 cards which are randomly
chosen from a\n",
"deck. The first card is shown face up. The game asks the player to predict
whether the next card\n",
"in the selection will have a higher or lower value than the currently showing
card.***\n",
"For example, say the card that’s shown is a 3. The player chooses “higher,”
and the next card is\n",
"shown. If that card has a higher value, the player is correct. In this
example, if the player had\n",
"chosen “lower,” they would have been incorrect. If the player guesses
correctly, they get 20\n",
"points. If they choose incorrectly, they lose 15 points. If the next card to
be turned over has the\n",
"same value as the previous card, the player is incorrect."
]
},
{
"cell_type": "markdown",
"id": "b032e353-b43a-44eb-9df3-a2d4a629a364",
"metadata": {},
"source": [
"***###############################################################################
#######################################***"
]
},
{
"cell_type": "code",
"execution_count": 22,
"id": "e5646b40-1109-4779-a12e-6b35a908c4bb",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"First card: 6 of Diamonds\n",
"Next card: 5 of Diamonds\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Higher or lower? Higher\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Incorrect! You lose 15 points.\n",
"Next card: 5 of Spades\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Higher or lower? lower\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Oops! The next card has the same value as the previous card.\n",
"Next card: 3 of Clubs\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Higher or lower? Higher\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Incorrect! You lose 15 points.\n",
"Next card: A of Spades\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Higher or lower? lower\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Incorrect! You lose 15 points.\n",
"Next card: A of Hearts\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Higher or lower? Higher\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Oops! The next card has the same value as the previous card.\n",
"Next card: 7 of Clubs\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Higher or lower? lower\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Correct! You gain 20 points.\n",
"Next card: 10 of Diamonds\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Higher or lower? lower\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Incorrect! You lose 15 points.\n",
"Game over! Your final score: -40 points.\n"
]
}
],
"source": [
"import random\n",
"\n",
"# Create a standard deck of cards (52 cards)\n",
"suits = [\"Hearts\", \"Diamonds\", \"Clubs\", \"Spades\"]\n",
"ranks =
[\"2\", \"3\", \"4\", \"5\", \"6\", \"7\", \"8\", \"9\", \"10\", \"J\", \"Q\", \"K\
", \"A\"]\n",
"deck = [(rank, suit) for suit in suits for rank in ranks]\n",
"\n",
"# Shuffle the deck\n",
"random.shuffle(deck)\n",
"\n",
"# Select 8 cards from the shuffled deck\n",
"selected_cards = deck[:8]\n",
"\n",
"# Show the first card face up\n",
"current_card = selected_cards.pop(0)\n",
"print(f\"First card: {current_card[0]} of {current_card[1]}\")\n",
"\n",
"# Game loop\n",
"score = 0\n",
"while selected_cards:\n",
" next_card = selected_cards.pop(0)\n",
" print(f\"Next card: {next_card[0]} of {next_card[1]}\")\n",
" \n",
" # Ask the player for their prediction\n",
" prediction = input(\"Higher or lower? \").lower()\n",
" \n",
" # Compare card values\n",
" if ranks.index(next_card[0]) > ranks.index(current_card[0]):\n",
" if prediction == \"higher\":\n",
" score += 20\n",
" print(\"Correct! You gain 20 points.\")\n",
" else:\n",
" score -= 15\n",
" print(\"Incorrect! You lose 15 points.\")\n",
" elif ranks.index(next_card[0]) < ranks.index(current_card[0]):\n",
" if prediction == \"lower\":\n",
" score += 20\n",
" print(\"Correct! You gain 20 points.\")\n",
" else:\n",
" score -= 15\n",
" print(\"Incorrect! You lose 15 points.\")\n",
" else:\n",
" print(\"Oops! The next card has the same value as the previous
card.\")\n",
" \n",
" current_card = next_card\n",
"\n",
"print(f\"Game over! Your final score: {score} points.\")\n"
]
},
{
"cell_type": "markdown",
"id": "1b759c3f-589e-40ed-8f00-03d4ebc14fa9",
"metadata": {},
"source": [
"***###############################################################################
#######################################***"
]
},
{
"cell_type": "markdown",
"id": "cc7fe447-7daa-4475-be2b-76b2a8930a25",
"metadata": {},
"source": [
"***Q5. Create an empty dictionary called Car_0 . Then fill the dictionary with
Keys : color , speed\r\n",
", X_position and Y_position.\r\n",
"car_0 = {'x_position': 10, 'y_position': 72, 'speed': 'medium'}*** .\r\n",
"a) If the speed is slow the coordinates of the X_pos get incremented by 2.\r\
n",
"b) If the speed is Medium the coordinates of the X_pos gets incremented by 9\
r\n",
"c) Now if the speed is Fast the coordinates of the X_pos gets incremented by
22.\r\n",
"Print the modified dictionary."
]
},
{
"cell_type": "code",
"execution_count": 23,
"id": "1ccabcc6-6f97-48f8-ac4f-ae5912154ed4",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'x_position': 19, 'y_position': 72, 'speed': 'medium', 'color': 'red'}\n"
]
}
],
"source": [
"Car_0 = {}\n",
"Car_0['x_position'] = 10\n",
"Car_0['y_position'] = 72\n",
"Car_0['speed'] = 'medium'\n",
"Car_0['color'] = 'red'\n",
"if Car_0['speed'] == 'slow':\n",
" Car_0['x_position'] += 2\n",
"elif Car_0['speed'] == 'medium':\n",
" Car_0['x_position'] += 9\n",
"elif Car_0['speed'] == 'fast':\n",
" Car_0['x_position'] += 22\n",
"print(Car_0)"
]
},
{
"cell_type": "markdown",
"id": "aee5eb07-f40f-4fd2-a6f6-3e4967bbd9de",
"metadata": {},
"source": [
"***###############################################################################
#######################################***"
]
},
{
"cell_type": "markdown",
"id": "d31e5d47-af30-4a13-9e54-92f28d4d073f",
"metadata": {},
"source": [
"***Q6. Show a basic implementation of abstraction in python using the abstract
classes.***\n",
"1. Create an abstract class in python.\n",
"2. Implement abstraction with the other classes and base class as abstract
class."
]
},
{
"cell_type": "code",
"execution_count": 25,
"id": "1671da55-26bb-49d0-98ad-44e073d8e2d7",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"I have 3 sides\n",
"I have 5 sides\n"
]
}
],
"source": [
"from abc import ABC, abstractmethod\n",
"\n",
"class Polygon(ABC):\n",
" @abstractmethod\n",
" def noofsides(self):\n",
" pass\n",
"\n",
"class Triangle(Polygon):\n",
" def noofsides(self):\n",
" print(\"I have 3 sides\")\n",
"\n",
"class Pentagon(Polygon):\n",
" def noofsides(self):\n",
" print(\"I have 5 sides\")\n",
"\n",
"class Hexagon(Polygon):\n",
" def noofsides(self):\n",
" print(\"I have 6 sides\")\n",
"\n",
"class Quadrilateral(Polygon):\n",
" def noofsides(self):\n",
" print(\"I have 4 sides\")\n",
"\n",
"# Example usage\n",
"triangle = Triangle()\n",
"triangle.noofsides() # Output: \"I have 3 sides\"\n",
"\n",
"pentagon = Pentagon()\n",
"pentagon.noofsides() # Output: \"I have 5 sides\"\n"
]
},
{
"cell_type": "markdown",
"id": "08b2d3dc-c091-45fd-9903-30195eef240d",
"metadata": {},
"source": [
"***###############################################################################
#######################################***"
]
},
{
"cell_type": "markdown",
"id": "417a2f0b-f367-4b2f-bfe8-386b09b69544",
"metadata": {},
"source": [
"***Q7. Create a program in python to demonstrate Polymorphism.***\r\n",
"1. Make use of private and protected members using python name mangling
techniques"
]
},
{
"cell_type": "code",
"execution_count": 26,
"id": "15638d05-7f6b-4a6f-b4dc-0f3dd2dc273e",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Woof!\n",
"Meow!\n",
"My name is Santhosh\n",
"Santhosh\n"
]
}
],
"source": [
"class Animal:\n",
" def speak(self):\n",
" raise NotImplementedError(\"Subclass must implement this method\")\n",
"\n",
"class Dog(Animal):\n",
" def speak(self):\n",
" return \"Woof!\"\n",
"\n",
"class Cat(Animal):\n",
" def speak(self):\n",
" return \"Meow!\"\n",
"\n",
"# Example usage\n",
"dog = Dog()\n",
"cat = Cat()\n",
"\n",
"print(dog.speak()) # Output: \"Woof!\"\n",
"print(cat.speak()) # Output: \"Meow!\"\n",
"\n",
"# Name mangling for private member\n",
"class Student:\n",
" def __init__(self, name):\n",
" self.__name = name\n",
"\n",
" def display_name(self):\n",
" print(f\"My name is {self.__name}\")\n",
"\n",
"s1 = Student(\"Santhosh\")\n",
"s1.display_name()\n",
"\n",
"# Accessing name-mangled variable\n",
"print(s1._Student__name) # Output: \"Santhosh\"\n"
]
},
{
"cell_type": "markdown",
"id": "12937933-5183-4da2-9d92-fceb74e6f8fc",
"metadata": {},
"source": [
"***###############################################################################
#######################################***"
]
},
{
"cell_type": "markdown",
"id": "431cfb13-bbf6-4ab8-8c3e-a189b51e0a4b",
"metadata": {},
"source": [
"***Q8. Given a list of 50 natural numbers from 1-50. Create a function that
will take every element\r\n",
"from the list and return the square of each element. Use the python map and
filter methods to\r\n",
"implement the function on the given lis***t."
]
},
{
"cell_type": "code",
"execution_count": 27,
"id": "3c6e9149-05e7-4cc1-befa-3dbb10834a0d",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[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]\n"
]
}
],
"source": [
"def square(num):\n",
" return num ** 2\n",
"\n",
"# Generate a list of natural numbers from 1 to 50\n",
"numbers = list(range(1, 51))\n",
"\n",
"# Use map to apply the square function to each element\n",
"squares = list(map(square, numbers))\n",
"\n",
"print(squares)\n"
]
},
{
"cell_type": "markdown",
"id": "5bc04e31-eb02-4804-872d-255c324a953f",
"metadata": {},
"source": [
"***###############################################################################
#######################################***"
]
},
{
"cell_type": "markdown",
"id": "4a72eca2-9a87-40aa-8aca-370ec5c8312c",
"metadata": {},
"source": [
"***Q9. Create a class, Triangle. Its init() method should take self, angle1,
angle2, and angle3 as\r\n",
"arguments***."
]
},
{
"cell_type": "code",
"execution_count": 28,
"id": "d5968c7d-9589-44a5-bafc-94b53476a9cb",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Angles: 60, 70, 50\n"
]
}
],
"source": [
"class Triangle:\n",
" def __init__(self, angle1, angle2, angle3):\n",
" self.angle1 = angle1\n",
" self.angle2 = angle2\n",
" self.angle3 = angle3\n",
"\n",
"# Example usage:\n",
"my_triangle = Triangle(60, 70, 50)\n",
"print(f\"Angles: {my_triangle.angle1}, {my_triangle.angle2},
{my_triangle.angle3}\")\n"
]
},
{
"cell_type": "markdown",
"id": "4637cc6f-ca14-4b48-9a93-73ee199b184e",
"metadata": {},
"source": [
"***###############################################################################
#######################################***"
]
},
{
"cell_type": "markdown",
"id": "3063321d-b5d2-4cbe-a38b-19233f30712f",
"metadata": {},
"source": [
"***Q10. Create a class variable named number_of_sides and set it equal to
3.***"
]
},
{
"cell_type": "code",
"execution_count": 41,
"id": "6ff70ac3-c07d-4b19-907b-e4a51decdd7e",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Number of sides in a Triangle: 3\n"
]
}
],
"source": [
"# Class variable \n",
"class Triangle:\n",
" number_of_sides = 3 \n",
" def __init__(self, angle1, angle2, angle3):\n",
" self.angle1 = angle1 \n",
" self.angle2 = angle2 \n",
" self.angle3 = angle3\n",
" \n",
" print(\"Number of sides in a Triangle:\", Triangle.number_of_sides)"
]
},
{
"cell_type": "markdown",
"id": "879232da-b559-459a-8b3d-3bdeb1ef2c49",
"metadata": {},
"source": [
"***###############################################################################
#######################################***"
]
},
{
"cell_type": "markdown",
"id": "4377eb99-c41b-4f36-a2a4-d5cc9a4f0c23",
"metadata": {},
"source": [
"***Q11. Create a method named check_angles. The sum of a triangle's three
angles should return\r\n",
"True if the sum is equal to 180, and False otherwise. The method should print
whether the\r\n",
"angles belong to a triangle or no***\n",
"11.1 Write methods to verify if the triangle is an acute triangle or obtuse
triangle.\r\n",
"11.2 Create an instance of the triangle class and call all the defined
methods.\r\n",
"11.3 Create three child classes of triangle class - isosceles_triangle,
right_triangle and\r\n",
"equilateral_triangle.\r\n",
"11.4 Define methods which check for their properties.t.t."
]
},
{
"cell_type": "code",
"execution_count": 73,
"id": "e1642fa0-f2f2-49fe-9ea1-80494469877e",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"True\n",
"True\n",
"True\n",
"True\n",
"True\n",
"True\n",
"True\n",
"True\n",
"True\n",
"False\n",
"True\n",
"False\n"
]
}
],
"source": [
"class Triangle:\n",
" def __init__(self, a, b, c):\n",
" self.a, self.b, self.c = a, b, c\n",
" print(self.is_right_triangle())\n",
" \n",
" def type_of_triangle(self):\n",
" if self.a < 90 and self.b < 90 and self.c < 90:\n",
" return \"Acute\"\n",
" return \"Obtuse\"\n",
" \n",
" def is_right_triangle(self):\n",
" if sum((self.a, self.b, self.c)) == 180:\n",
" return True\n",
" return False\n",
"\n",
"\n",
"class Isosceles_triangle(Triangle):\n",
" def __init__(self, a, b, c):\n",
" super().__init__(a, b, c)\n",
" \n",
" def check(self):\n",
" if self.a == self.b or self.b == self.c or self.c == self.a:\n",
" return True\n",
" return False\n",
"\n",
"\n",
"class Right_triangle(Triangle):\n",
" def __init__(self, a, b, c):\n",
" super().__init__(a, b, c)\n",
" \n",
" def check(self):\n",
" if self.a == 90 or self.b == 90 or self.c == 90:\n",
" return True\n",
" return False\n",
"\n",
"\n",
"class Equilateral_triangle(Triangle):\n",
" def __init__(self, a, b, c):\n",
" super().__init__(a, b, c)\n",
" \n",
" def check(self):\n",
" if self.a == self.b == self.c:\n",
" return True\n",
" return False\n",
"\n",
"\n",
"# TESTS:\n",
"t1 = Isosceles_triangle(60, 60, 60)\n",
"print(t1.check())\n",
"t2 = Right_triangle(90, 30, 60)\n",
"print(t2.check())\n",
"t3 = Equilateral_triangle(60, 60, 60)\n",
"print(t3.check())\n",
"t1 = Isosceles_triangle(70, 55, 55)\n",
"print(t1.check())\n",
"t2 = Right_triangle(60, 60, 60)\n",
"print(t2.check())\n",
"t3 = Equilateral_triangle(60, 59, 61)\n",
"print(t3.check())"
]
},
{
"cell_type": "markdown",
"id": "8f6ea6fc-de29-47f2-8e57-13c1b29d4778",
"metadata": {},
"source": [
"***###############################################################################
#######################################***"
]
},
{
"cell_type": "markdown",
"id": "6eb8795a-ae0e-49a3-81f1-c8bf9eb5f45f",
"metadata": {},
"source": [
"***Q12. Create a class isosceles_right_triangle which inherits from
isosceles_triangle and\n",
"right_triangle.***\n",
"12.1 Define methods which check for their properties."
]
},
{
"cell_type": "code",
"execution_count": 77,
"id": "87ab6b72-7393-4ee2-b757-97a7c534c860",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Is Isosceles Right Triangle: True\n"
]
}
],
"source": [
"class Triangle: \n",
" def __init__(self, angle1, angle2, angle3): \n",
" self.angle1 = angle1 \n",
" self.angle2 = angle2 \n",
" self.angle3 = angle3\n",
"\n",
"class IsoscelesTriangle(Triangle): \n",
" def is_isosceles(self):\n",
" if self.angle1 == self.angle2 or self.angle2 == self.angle3 or
self.angle3 == self.angle1:\n",
" return True\n",
" return False\n",
" \n",
"class RightTriangle(Triangle):\n",
" def is_right_triangle(self): \n",
" if any(angle == 90 for angle in [self.angle1, self.angle2,
self.angle3]):\n",
" return True \n",
" return False\n",
" \n",
"class IsoscelesRightTriangle(IsoscelesTriangle, RightTriangle):\n",
" def __init__(self, angle1, angle2):\n",
" super().__init__(angle1, angle2, 180 - (angle1 + angle2)) \n",
" \n",
" def is_isosceles_right_triangle(self): \n",
" return self.is_isosceles() and self.is_right_triangle()\n",
"# Create an instance of IsoscelesRightTriangle\n",
"isosceles_right = IsoscelesRightTriangle(45, 45)\n",
"# Check its properties\n",
"print(\"Is Isosceles Right Triangle:\",
isosceles_right.is_isosceles_right_triangle())"
]
},
{
"cell_type": "markdown",
"id": "ba8a2ad1-4396-46b5-b7b9-fb68dc5ae992",
"metadata": {},
"source": [
"***###############################################################################
#######################################***"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3646d332-b158-402a-aaa1-9666a02ba5f6",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.5"
}
},
"nbformat": 4,
"nbformat_minor": 5
}