Ask Question
12 October, 21:33

In Python, The sum of the elements in a tuple can be recusively calculated as follows:The sum of the elements in a tuple of size 0 is 0Otherwise, the sum is the value of the first element added to the sum of the rest of the elementsWrite a function named sum that accepts a tuple as an argument and returns the sum of the elements in the tuple.

+3
Answers (1)
  1. 12 October, 21:42
    0
    Following are the program in the Python Programming Language.

    # Define the function

    def Sum (tu):

    # Check if the tuple contain 0

    if len (tu) = =0:

    #Then, Return 0

    return 0

    #Otherwise

    else:

    #call the recursive function

    return tu[0]+Sum (tu[1:])

    #Set tuple type variable

    tu = (2,5,1,8,10)

    #print and call the function

    print ("The sum of tuple is:", Sum (tu))

    Output:

    The sum of tuple is: 26

    Explanation:

    Here, we define a function "sum () " and pass an argument "tu" which stores the tuple type value, inside the function.

    Set the if conditional statement to check condition is the length of the tuple is 0 then, return 0. Otherwise, call and return the sum of the tuple which is recursively calculated and close the function.

    Finally, set the tuple type variable "tu" and initialize the value in it then, print and call the function sum.
Know the Answer?
Not Sure About the Answer?
Get an answer to your question ✅ “In Python, The sum of the elements in a tuple can be recusively calculated as follows:The sum of the elements in a tuple of size 0 is ...” in 📙 Computers & Technology if there is no answer or all answers are wrong, use a search bar and try to find the answer among similar questions.
Search for Other Answers