Assignment 6 Utkarsh
Assignment 6 Utkarsh
Question 1
In [1]:
class Triangle:
"""
Defining Class Triangle Constructor method 3 sides
"""
def isTriangle(self):
"""
for triangle to form sum of any two sides
should be greater than third this function checks this
"""
if (self.a + self.b)>self.c and (self.b + self.c)>self.a and (self.a + self.c)>s
elf.b:
x = True
else:
x = False
return x
def perimeter(self):
"""
This function returns perimeter of triangle
p = a+b+c
only if sides a,b,c form a triangle
"""
if self.isTriangle()==True:
p = self.a + self.b + self.c
else:
p = None
print(f'The sides {self.a}, {self.b}, {self.c} do not form a triangle')
return p
def area(self):
"""
This funtion returns area of triangle based on
Herons formula
only if sides a,b,c form a triangle
"""
if self.isTriangle()==True:
s = (self.a+self.b+self.c)/2 #Semi-perimeter calculation
ar = (s*(s-self.a)*(s-self.b)*(s-self.c))**0.5
else:
ar = None
print(f'The sides {self.a}, {self.b}, {self.c} do not form a triangle')
return ar
In [2]:
t1 = Triangle(3,4,5)
In [3]:
print(type(t1))
<class '__main__.Triangle'>
In [4]:
print(f'Sides of object t1 of class Triangle is {t1.a}, {t1.b}, {t1.c}')
In [5]:
t1.isTriangle()
Out[5]:
True
In [6]:
t1.perimeter()
Out[6]:
12
In [7]:
t1.area()
Out[7]:
6.0
In [8]:
t2 = Triangle(1,2,3)
print(t2.isTriangle())
False
In [9]:
t2.perimeter()
The sides 1, 2, 3 do not form a triangle
In [10]:
t2.area()
Question 2
These four pillars are essential elements of object-oriented programming and work together to allow for the
creation of robust, flexible, and scalable software systems.
Question 3
In [12]:
# Defining a seltos car object and printing each attribute
seltos = car('Kia',1700,'black')
print(seltos.manufacturer)
print(seltos.weight)
print(seltos.color)
Kia
1700
black
Question 4
It is used to access the attributes and methods of the object within the
class
In [13]:
# Consider above example with car class I added a method in this class called details whi
ch uses self
class car:
"""
Definining a class car with initialized
Manufactured, weight and color attributes
"""
def details(self):
print(f'This car was manufactured by {self.manufacturer}, its weight is {self.wei
ght} and its color is {self.color} ')
In [14]:
# Explaining Use of above code with car class
# Explaining Use of above code with car class
hector = car('Morris Garages',1600,'Glaze Red')
hector.details()
This car was manufactured by Morris Garages, its weight is 1600 and its color is Glaze Re
d
In above example the 'details()' method uses 'self' to access cars 'manufacturer', 'weight'
and 'color'.
Without 'self', method 'details()' is not able to create any attribute and will give error.
Question 5
The new class inherits the attributes and behaviors of the existing class,
and can also add its own unique attributes and behaviors.
1. Single Inheritance : When child class is derived from only one parent class. This is called single inheritance
In [15]:
# Single Inheritance Example
class Brands: #parent_class
brand_name_1 = "Amazon"
brand_name_2 = "Ebay"
brand_name_3 = "OLX"
1. Multiple Inheritance: When child class is derived or inherited from more than one parent class. This is called
multiple inheritance. In multiple inheritance, we have two parent classes/base classes and one child class
that inherits both parent classes properties.
In [16]:
#example_of_multiple_inheritance
class Popularity(Brands,Products): #child class created from parent class 1 and parent cl
ass 2
prod_1_popularity = 100
prod_2_popularity = 70
prod_3_popularity = 60
1. Multilevel Inheritance: In multilevel inheritance, we have one parent class and child class that is derived or
inherited from that parent class. We have a grand-child class that is derived from the child class.
In [17]:
#example_of_multilevel_inheritance
1. Hierarchical inheritance: When we derive or inherit more than one child class from one(same) parent class.
Then this type of inheritance is called hierarchical inheritance
In [18]:
#example of Hierarchical inheritence
Amazon is an Online Ecommerce Store with popularity of 100 and is Excellent Value
Ebay is an Online Store with popularity of 70 and is Better Value
OLX is an Online Buy Sell Store with popularity of 60 and is Good Value
1. Hybrid Inheritance - Inheritance consisting of multiple types of inheritance is called hybrid inheritance.
In [19]:
# hybrid inheritance
class School:
def func1(self):
print("This function is in school.")
class Student1(School):
def func2(self):
print("This function is in student 1. ")
class Student2(School):
def func3(self):
print("This function is in student 2.")
# Driver's code
object = Student3()
object.func1()
object.func2()
object.func4()