Tuples are a data structure used in Python to store an ordered collection of objects. When working with tuples, it might be handy to know how to quickly compute the mean. In this tutorial, we will look at how to get the average of a tuple with numeric values in Python.
How to find the average of a tuple in Python?
You can use a combination of the Python built-in
len() functions to get the average of a tuple. Alternatively, you can also use methods from libraries like
Let’s look at these methods with the help of some examples.
To get the tuple mean, compute the tuple sum using the
sum() function and divide it by the length of the tuple (which you can get using the
Here’s an example.
# create a tuple t = (10, 20, 30, 40) # get average of tuple values print(sum(t)/len(t))
We get the average of the tuple containing the values 10, 20, 30, and 40 as 25.
You can also compute the mean of a tuple directly using off-the-shelf methods from some libraries like
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 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.
You can use the
statistics standard library’s
mean() function to get the mean of a tuple. Pass the tuple as an argument to the function.
Here’s an example.
import statistics # create a tuple t = (10, 20, 30, 40) # get average of tuple values print(statistics.mean(t))
We get the same result as above.
For more on the
statistics library, refer to its documentation.
You can also use the
numpy library to get the average of values in a tuple. Pass the tuple as an argument to the library’s
Let’s look at an example.
import numpy as np # create a tuple t = (10, 20, 30, 40) # get average of tuple values print(np.mean(t))
We get 25 as the mean, the same result that we got with the methods above.
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.