1 classSquare:2 def __init__(Self, WH):#because it is a square, it takes only one side of the length3 ifisinstance (WH, (int,float)):4SELF.WH =WH5 Else:6 RaiseTypeError7 8 def __add__(self, Other):9Self_area = SELF.WH * SELF.WH#Calculate self AreaTenOther_area = OTHER.WH * OTHER.WH#Calculate other Area One returnSelf_area + Other_area#Add X+y A - def __sub__(self,other): -Self_area = SELF.WH * SELF.WH#Calculate self Area theOther_area = OTHER.WH * OTHER.WH#Calculate other Area - returnSelf_area-other_area#subtract x-y - - def __mul__(self,other): +Self_area = SELF.WH * SELF.WH#Calculate self Area -Other_area = OTHER.WH * OTHER.WH#Calculate other Area + returnSelf_area * Other_area#Multiply X*y A at def __mod__(self,other): -Self_area = SELF.WH * SELF.WH#Calculate self Area -Other_area = OTHER.WH * OTHER.WH#Calculate other Area - returnSelf_area% Other_area#Divide x%y - -S1 = Square (3) inS2 = Square (4)
Python Fundamentals: Implementing a subtraction of a square class