How to write a function in Python to perform subtraction on numbers that are given as arguments? for eg, subt(a, b, c…) O/P : value of a-b-c-

Question:

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
Asked By: Naveen Kaulwar

||

Answers:

What you are looking for is *args argument.

def subt(*num):
    diff = num[0]
    for i in range(1,len(num)):
        diff = diff - num[i]
    return diff

What does the asterisk in front of the argument do? It takes as many arguments you give and wraps them in a list object. Thus

diff(1,2,3) #=> *num = [1,2,3]

PythonTips have a full Section on *args and **kwargs.

Answered By: Cpt.Hook

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.

Answered By: Naveen Kaulwar

The upper answer is correct, but I want to make it a bit more pythonious!

def subt(*nums):
    first_num = nums[0] # The first number
    for num in nums[1:]: # index 1 to the last number
        first_num -= num
    return first_num

print(subt(33, 2, 3, 4, 7)) # -> 17
print(subt(13, 7)) # -> 6

Method 2

def subt(*nums):
    first_num = nums[0] # The first number
    sum_of_rest = sum(nums[1:]) # The sum of all the number except the first number
                                # Or you can say, sum of numbers from index 1 to the last

    return first_num - sum_of_rest

print(subt(33, 2, 3, 4, 7)) # -> 17
print(subt(13, 7)) # -> 6
Answered By: Someone193