Write a program to create function calculation() such that it can accept two variables and calculate addition and subtraction. Also, it must return both addition and subtraction in a single return call.
Given:
defcalculation(a, b):# Your Code
res = calculation(40,10)print(res)
In Python, to return multiple values from a function, use a comma to separate them.
Solution1:
def calculation(a, b):
addition = a + b
subtraction = a - b
# return multiple values separated by comma
return addition, subtraction
# get result in tuple format
res = calculation(40, 10)
print(res)
Solution2:
def calculation(a, b):
return a + b, a - b
# get result in tuple format
# unpack tuple
add, sub = calculation(40, 10)
print(add, sub)
Hint:
Separate return values with a comma.
Solution:
In Python, to return multiple values from a function, use a comma to separate them.
Solution1:
Solution2:
need an explanation for this answer? contact us directly to get an explanation for this answer