sum of elements in a tuple in python

Sum of Elements in a Tuple in Python

When working with tuples, it can be handy to know how to quickly get the sum of values in a tuple. For example, you have a tuple containing taxes paid by you in the last five years and you want to calculate the total tax paid. In this tutorial, we will look at how to get the sum of the elements in a tuple in Python with the help of some examples.

sum of elements in a tuple in python

Tuples, similar to lists, are data structures used to store an ordered collection of objects in Python. But unlike lists which are very flexible, objects in a tuple cannot be altered.

You can use the python built-in sum() function to get the sum of tuple elements. Alternatively, you can use a loop to iterate through the items and use a variable to keep track of the sum.

Let’s look at the above-mentioned methods with the help of some examples.

The built-in sum() function in Python is used to return the sum of an iterable. To get the sum total of a tuple of numbers, you can pass the tuple as an argument to the sum() function.

# create a tuple
t = (1, 2, 3, 4)
# sum of tuple elements
print(sum(t))

Output:

10

We get the sum of the values in the tuple as a scaler value.

Note that the sum() function may result in loss of precision with extended sums of floating-point numbers. For example –

📚 Data Science Programs By Skill Level

Introductory

Intermediate ⭐⭐⭐

Advanced ⭐⭐⭐⭐⭐

🔎 Find Data Science Programs 👨‍💻 111,889 already enrolled

Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.

# create a tuple
t = (0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1)
# sum of tuple elements
print(sum(t))

Output:

0.8999999999999999

As an alternative, you can use the math standard library’s fsum() function to get an accurate sum of floating-point numbers and prevent loss of precision.

import math

# create a tuple
t = (0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1)
# sum of tuple elements
print(math.fsum(t))

Output:

0.9

We get the accurate result this time. For more on the fsum() function, refer to its documentation.

Alternatively, you can use the straightforward method of iterating through the tuple elements and keeping track of the sum.

# create a tuple
t = (1, 2, 3, 4)
# use a loop to get the sum
total = 0
for item in t:
    total += item
print(total)

Output:

10

We get the sum of the values in the tuple.

You might also be interested in –


Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.


Author

  • Piyush Raj

    Piyush is a data professional passionate about using data to understand things better and make informed decisions. He has experience working as a Data Scientist in the consulting domain and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.

Scroll to Top