Require to write a Python function to perform the subtraction operation on multiple numbers (left to right direction) given as arguments.
User should be able to give a variable number of arguments to that function.
For eg, subt(a, b, c…) must return the value of a-b-c-… where a, b, c are the numbers given as arguments to the function
Initially I wrote a function to perform subtraction operation on two numbers as below:
def subt(a, b):
return a-b
later, I extended it for three numbers as below:
def subt(a, b, c):
return a-b-c
Now I want to extend the above function for variable number of arguments but do not know how to proceed from below:
def subt(…):
diff =
for i in range(…,len(…)):
diff = diff - […]
return diff
There are two methods to solve this:
Method-1
Using the logic for subtraction a-b-c-… = ((a-b)-c)-…
def subt1(*numbers): # defining a function subt1 and using a non-keyword argument *numbers so that variable number of arguments can be provided by user. All these arguments will be stored as a tuple.
try: # using try-except to handle the errors. If numbers are given as arguments, then the statements in the try block will get executed.
diff = numbers[0] # assigning the first element/number to the variable diff
for i in range(1,len(numbers)): # iterating through all the given elements/ numbers of a tuple using a for loop
diff = diff - numbers[i] # performing the subtraction operation for multiple numbers from left to right, for eg, a-b-c = (a-b)-c
return diff # returning the final value of the above operation
except: # if no arguments OR more than one non-numbers are passed, then the statement in the except block will get executed
return 'please enter numbers as arguments'
subt1(10, 5, -7, 9, -1) ----> here subt1 performs 10-5-(-7)-9-(-1) and returns the value
4
subt1(25.5, 50.0, -100.25, 75) ----> here subt1 performs 25.5-50.0-(-100.25)-75 and returns the value
0.75
subt1(20j, 10, -50+100j, 150j) ----> here subt1 performs 20j-10-(-50+100j)-150j and returns the value
(40-230j)
subt1() ----> here the statement in the except block is returned as no input is passed
'please enter numbers as arguments'
subt1('e', 1, 2.0, 3j) ---> here the statement in the except block is returned as a string 'e' is passed which is not a number
'please enter numbers as arguments'
Method-2
Using the logic for subtraction a-b-c-… = a-(b+c+…) = a-add(b,c,…)
def subt2(*numbers):
try:
add = 0 # initializing a variable add with 0
for i in range(1,len(numbers)):
add = add+ numbers[i] # performing the addition operation for the numbers starting from the index 1
return numbers[0]-add # returning the final value of subtraction of given numbers, logic : a-b-c = a-(b+c) = a-add(b,c)
except:
return 'please enter numbers as arguments'
subt2(10, 5, -7, 9, -1) ----> here subt2 performs 10-5-(-7)-9-(-1) and returns the value
4
subt2(25.5, 50.0, -100.25, 75) ----> here subt2 performs 25.5-50.0-(-100.25)-75 and returns the value
0.75
subt2(20j, 10, -50+100j, 150j) ----> here subt2 performs 20j-10-(-50+100j)-150j and returns the value
(40-230j)
Note : All the above test cases have been tested in Jupyter notebooks.