Package: addition-subtraction
Python Class for Dynamically Switching Between Addition and Subtraction Operations: AddSubIntroductionIn this tutorial, we will create a Python class namedAddSubthat allows us to dynamically switch between addition and subtraction operations. This class provides a flexible way to perform different operations on numbers based on a specified method.Code ImplementationclassAddSub:def__init__(self,method_val:int=1)->None:"""This is the constructor of the AddSub class.It takes an optional parameter method_val, which specifies the default method to be used for operations.If method_val is not an integer, it is set to 1 (addition) by default.If method_val is less than 0 or greater than 1, it is also set to 1 by default."""iftype(method_val)!=type(1):self.method=1print('only integer elements accepted')elif((method_val<=1)and(method_val>=0)):self.method=method_valelse:self.method=1print("Please enter a valid method by default addition operation has set")defoperation(self,first_number:int=0,second_number:int=0)->int:"""This method performs the operation specified by the method attribute on the two given numbers.If the method attribute is 0, subtraction is performed.If the method attribute is 1, addition is performed."""if(self.method==0):returnfirst_number-second_numberelif(self.method==1):returnfirst_number+second_numberdefchange_method(self,method_val:int)->None:"""This method changes the method attribute to the given value.If the given value is not an integer, the method attribute is not changed.If the given value is less than 0 or greater than 1, the method attribute is also not changed."""if(type(method_val)!=type(1)):print('only integer elements accepted, previous method has unchanged')elif((method_val<=1)and(method_val>=0)):self.method=method_valelse:print("Please enter a valid method, previous method has unchanged")# Usage:# Creating an instance of the AddSub classadd_sub=AddSub()# Performing addition using the default methodresult=add_sub.operation(10,20)print("Addition result:",result)# Output: 30# Changing the method to subtractionadd_sub.change_method(0)# Performing subtraction using the new methodresult=add_sub.operation(20,10)print("Subtraction result:",result)# Output: 10# Changing the method to an invalid valueadd_sub.change_method(2)# Performing addition using the previous method (addition)result=add_sub.operation(100,200)print("Addition result (using previous method):",result)# Output: 300ExplanationThe__init__method is the constructor of theAddSubclass. It takes an optional parametermethod_valthat specifies the default method to be used for operations.In the__init__method, we check ifmethod_valis an integer. If it is not, we print an error message and setmethodto 1 (addition).We also check ifmethod_valis within the valid range of 0 (subtraction) and 1 (addition). If it is not, we print an error message and setmethodto 1.Theoperationmethod takes two integer parametersfirst_numberandsecond_numberand performs the operation specified by themethodattribute on the two given numbers.Thechange_methodmethod takes an integer parametermethod_valand changes themethodattribute to the given value, provided that it is a valid value.In the usage section, we create an instance of theAddSubclass and perform addition and subtraction operations using the default method.We then change the method to subtraction using thechange_methodmethod and perform a subtraction operation.We try to change the method to an invalid value, but thechange_methodmethod prevents this and prints an error message.Finally, we perform an addition operation using the previous method (addition) and demonstrate that the method has not changed.ConclusionTheAddSubclass provides a flexible way to dynamically switch between addition and subtraction operations. This can be useful in various scenarios where you need to perform different operations based on certain conditions or user input.