Program to find Sum of Tuple Items

Python Program to find Sum of Tuple Items

In the previous article, we have discussed Python Program to Find the Sum of Series 1+X+X^2/2…+X^N/N

Given a tuple and the task is to find the sum of the given tuple elements.

Tuple in Python:

A tuple is an immutable list of objects. That means the elements of a tuple cannot be modified or changed while the program is running.

Examples:

Example1:

Input:

Given Tuple = (12, 1, 45, 20, 10, 15, 35)

Output:

The Sum of the given tuple elements (12, 1, 45, 20, 10, 15, 35) = 138

Example2:

Input:

Given Tuple = (30, 23, 65, 13, 45, 67, 89)

Output:

The Sum of the given tuple elements (30, 23, 65, 13, 45, 67, 89) = 332

Program To Find Sum of Tuple Items in Python

Below are the ways to find the sum of given tuple elements:

Method #1: Using For Loop (Static Input)

Approach:

  • Give the tuple as static input and store it in a variable.
  • Take a variable say sum_tupl and initialize its value to 0.
  • Loop in the given tuple using the for loop.
  • Add the iterator value to the above-initialized sum_tupl and store it in the same variable sum_tupl.
  • Print sum_tupl to get the sum of given tuple elements.
  • The Exit of the program.

Below is the implementation:

# Give the tuple as static input and store it in a variable.
gven_tupl = (12, 1, 45, 20, 10, 15, 35)
# Take a variable say sum_tupl and initialize its value to 0.
sum_tupl = 0
# Loop in the above given tuple using the for loop.
for elemt in gven_tupl:
  # Add the iterator value to the above-initialized sum_tupl and store it in the same
    # variable sum_tupl.
    sum_tupl = sum_tupl + elemt
# Print sum_tupl to get the sum of given tuple elements.
print("The Sum of the given tuple elements", gven_tupl, "=", sum_tupl)

Output:

The Sum of the given tuple elements (12, 1, 45, 20, 10, 15, 35) = 138

Method #2: Using For loop (User Input)

Approach:

  • Give the tuple as user input using tuple(),map(),input(),and split() functions and Store it in a variable.
  • Take a variable say sum_tupl and initialize its value to 0.
  • Loop in the given tuple using the for loop.
  • Add the iterator value to the above-initialized sum_tupl and store it in the same variable sum_tupl.
  • Print sum_tupl to get the sum of given tuple elements.
  • The Exit of the program.

Below is the implementation:

# Give the tuple as user input using tuple(),map(),input(),and split() functions
# and Store it in a variable.
gven_tupl = tuple(map(int, input( 'Enter some random tuple Elements separated by spaces = ').split()))
# Take a variable say sum_tupl and initialize its value to 0.
sum_tupl = 0
# Loop in the above given tuple using the for loop.
for elemt in gven_tupl:
  # Add the iterator value to the above-initialized sum_tupl and store it in the same
    # variable sum_tupl.
    sum_tupl = sum_tupl + elemt
# Print sum_tupl to get the sum of given tuple elements.
print("The Sum of the given tuple elements", gven_tupl, "=", sum_tupl)

Output:

Enter some random tuple Elements separated by spaces = 25 34 89 75 16 3 2
The Sum of the given tuple elements (25, 34, 89, 75, 16, 3, 2) = 244

Method #3: Using sum() Function (Static Input)

Approach:

  • Give the tuple as static input and store it in a variable.
  • Calculate the sum of given tuple elements using the sum() function and store it in another variable.
  • Print the sum of the above-given tuple elements.
  • The Exit of the program.

Below is the implementation:

# Give the tuple as static input and store it in a variable.
gven_tupl = (10, 20, 45, 63, 72, 91)
# Calculate the sum of given tuple elements using the sum() function and
# store it in another variable.
sum_tupl = sum(gven_tupl)
# Print the sum of the above-given tuple elements.
print("The Sum of the given tuple elements", gven_tupl, "=", sum_tupl)

Output:

The Sum of the given tuple elements (10, 20, 45, 63, 72, 91) = 301

Method #4: Using sum() Function (User Input)

Approach:

  • Give the tuple as user input using tuple(),map(),input(),and split() functions and Store it in a variable.
  • Calculate the sum of given tuple elements using the sum() function and store it in another variable.
  • Print the sum of the above-given tuple elements.
  • The Exit of the program.

Below is the implementation:

# Give the tuple as user input using tuple(),map(),input(),and split() functions and
# Store it in a variable.
gven_tupl = tuple(map(int, input( 'Enter some random tuple Elements separated by spaces = ').split()))
# Calculate the sum of given tuple elements using the sum() function and
# store it in another variable.
sum_tupl = sum(gven_tupl)
# Print the sum of the above-given tuple elements.
print("The Sum of the given tuple elements", gven_tupl, "=", sum_tupl)

Output:

Enter some random tuple Elements separated by spaces = 1 2 6 7 9 6
The Sum of the given tuple elements (1, 2, 6, 7, 9, 6) = 31

Explore more Example Python Programs with output and explanation and practice them for your interviews, assignments and stand out from the rest of the crowd.