Ask Question
19 June, 15:29

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.

+5
Answers (1)
  1. 19 June, 18:25
    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?
Find 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 and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers