In this article, we will discuss what is the arithmetic mean, and how to calculate mean in python. Later in the article, we will discuss how to use statistics.mean() function in python to calculate the mean.

If you are a data science or machine learning aspirant, you must know how important statistics is to learn the concepts. Statistics concepts help to answer the questions such as: what are the most important features for analysis, which performance metric we should measure, what are expected outcomes, and how to differentiate valid and noise data, etc.

In simple terms, it helps with Data Exploration, Data Cleaning, Model Selection, Evaluation, and finally, hyperparameter tuning to improve the machine learning model.

When it comes to data analysis or data exploration, we mainly use mean, median, and mode.

In this article, we will learn how to calculate the data’s mean (arithmetic mean) using Python. We have a statistics module in Python from which we will import mean().

Now, let’s dive into learning how to use statistics.mean() function to find the mean of the data with the help of examples.

**Must Read:** Modules in Python

**Must Read:** Maths for Data Science

**What is Mean?**

Mean, or Arithmetic Mean, is the sum of observations divided by the number of observations, i.e., if we have *n* observations *x _{1}, x_{2}, x_{3},……, x_{n}*, then:

**Example -1: Find the mean of 2, 3, 4, 5, 6.**

**Answer:**

sum of observations = 2 + 3 + 4 + 5 + 6 = 20

number of observations = 5

Hence, Mean = 20 / 5 = 4.

**Must Check:** Measure of Central Tendency: Mean, Median, and Mode

Till now, we have learned what is mean and how to calculate the mean manually. Now let’s see how Python’s mean() function is used.

**How to use statistics.mean() function?**

**Syntax**

import statisticsstatistics.mean([data-set])

**or**

from statistics import meanmean([data-set])

where,

[dataset]: list or tuple containing the set of number

**Return Value**

- mean() function returns the arithmetic mean of the dataset
- It will return TypeError if the list or the tuple doesn’t have a numeric value.
- It will return TypeError if no argument is passed.

Now, let’s have an example.

**Find the mean of the data using statistics.mean() function.**

We will take the same data as in the above example.

#import statistics module
import statistics
data = [2, 3, 4, 5, 6]
#print meanprint("The mean of the data is:", statistics.mean(data))

**Output**

Programming Online Courses and Certification | Python Online Courses and Certifications |

Data Science Online Courses and Certifications | Machine Learning Online Courses and Certifications |

**How to find the mean using for loop in Python?**

We will take the same data as in example 1.

# input datadata = [2, 3, 4, 5, 6]
# initialize variables
sum_of_observations = 0number_of_observations = 0
# now, iterate through list and find the sum of observations and number of observations
for i in data: sum_of_observations += i number_of_observations += 1
mean = sum_of_observations/number_of_observations #print
print("The mean of the data is:", mean)

**Output**

**Also Read:** for loop in Python

**How to find the mean using sum() and len() functions in Python?**

First, we will define the function and then use the sum() and len() functions.

# define the datasetdata = [2, 3, 4, 5, 6]
#define a function to calculate the mean
def ar_mean(data): mean = sum(data)/len(data) return mean
# print
print("the mean of the data is:", mean)

**Output**

**Must Read:** def() keyword in Python

**Must Read:** len() function in Python

**How to Calculate the mean of Dictionary in Python?**

In the dictionary, the statistics.mean() only counts the key as a number and returns the mean.

#import statstics modulefrom statistics import mean
#define dictionary
data = {2:4, 3:9, 4:16, 5:25, 6:36}
#print
print("The Mean of the data is:", mean(data))

**Output**

**Also Read:** Python Dictionary

Till now, we have seen several example, in which we have passed list, dictionary to find the mean of the data. But what if we will not pass any argument in the statistics.mean() function. Let’s see the next example.

**Calculate the mean without passing an argument.**

#calculate mean without passing argument in statistics.mean()
from statistics import mean
mean([])

**Output**

Recursion Function in Python | count() Function in Python |

len() Function in Python | float() Function in Python |

range() Function in Python | lambda() Function in Python |

**Conclusion**

In this article, we have discussed what is mean in statistics, and how to calculate mean in python. Later in the article, we will discuss how to use statistics.mean() function in python to calculate the mean.

Hope you will like the article.

Keep Learning!!

Keep Sharing!!

**About the Author**

Vikram has a Postgraduate degree in Applied Mathematics, with a keen interest in Data Science and Machine Learning. He has experience of 2+ years in content creation in Mathematics, Statistics, Data Science, and Mac... Read Full Bio