AppDividend
Latest Code Tutorials

Python mean Example | Python Statistics mean() Function Tutorial

0

Python mean Example | Python Statistics mean() Function Tutorial is today’s topic. Python is a popular language when it comes to data analysis and statistics. Luckily, Python 3 provides statistics module, which comes with very useful functions like mean(), median()mode() etc. The arithmetic mean is the sum of data that is divided by the number of data points. It is the measure of the central location of data in a set of values which vary in range.

Python mean Example

Python statistics mean function can be used to calculate the mean/average of the given list of numbers. It returns the mean of the data set passed as parameters.

In Python, we usually do the dividing of the sum of given numbers with the count of number present inside the list, tuple or dictionary.

First, we need to import the Python statistics module and then we can use the mean function to return the mean of the given list. See the following example.

#Python Statistics Module

The Statistics module provides functions for calculating mathematical statistics of numeric (Real-valued) data. Let’s see some examples of the Statistics module.

# app.py

import statistics

data = [11, 21, 11, 19, 46, 21, 19, 29, 21, 18, 3, 11, 11]

x = statistics.mean(data)
print(x)

y = statistics.median(data)
print(y)

z = statistics.mode(data)
print(z)

a = statistics.stdev(data)
print(a)

b = statistics.variance(data)
print(b)

See the following output.

➜  pyt python3 app.py
18.53846153846154
19
11
10.611435534486562
112.6025641025641
➜  pyt

In the above code example, we have used Mean, mode, median, variance, stddev functions.

#Python mean()

We have taken the SPIs from 1st semester to 8th semester. We will calculate the mean of that SPIs.

# app.py

import statistics

spiList = [5.55, 5.72, 7.3, 7.75, 8.4, 9, 8.8, 8.2]
print(statistics.mean(spiList))

See the following output.

Python mean Function Example

In the above example, we have eight data points.

#More Examples

Let’s add the following code inside the app.py file.

# app.py

import statistics

data = [21, 19, 18, 46, 30]
print(statistics.mean(data))

See the below output.

Python Statistics Tutorial | mean Function Example

#Calculate Mean of a tuple

Let’s calculate the mean of the tuple using the following code.

# app.py

import statistics

tupleA = (1, 9, 2, 1, 1, 8)
print(statistics.mean(tupleA))

The output of the above code is following.

Calculate Mean of a tuple

It will work the same as a list. It merely returns the Mean of the numbers inside the tuple.

#Calculate Mean of a Dictionary

In Dictionary, the mean function only counts the keys as the numbers and returns the mean of that dictionary based on the dictionary keys.

# app.py

import statistics

dictA = {1: 19, 2:21, 3:18, 4:46, 5:30}
print(statistics.mean(dictA))

See the below output.

Calculate Mean of a Dictionary

#Calculate mean of a tuple of a negative set of integers

Let’s see the tuple of negative integers.

# app.py

import statistics

data = (-11, -21, -18, -19, -46)
print(statistics.mean(data))

See the following output.

➜  pyt python3 app.py
-23
➜  pyt

Let’s take an example of the tuple of a mixed range of numbers.

# app.py

import statistics

data = (11, 21, -18, 19, -46)
print(statistics.mean(data))

See the following output.

➜  pyt python3 app.py
-2.6
➜  pyt

#Calculate mean of a list of a negative set of integers

Let’s take a list of a negative set of integers and apply the mean() function on it.

# app.py

import statistics

data = [-11, -21, -18, -19, -46]
print(statistics.mean(data))

See the following output.

➜  pyt python3 app.py
-23
➜  pyt

Let’s take an example of the list of a mixed range of numbers.

# app.py
import statistics

data = [11, 21, -18, -19, 46]
print(statistics.mean(data))

See the output.

➜  pyt python3 app.py
8.2
➜  pyt

#Calculate the mean of the list of fractions

See the following code.

# app.py

import statistics
from fractions import Fraction as fr

data = [fr(1, 2), fr(44, 12), fr(10, 3), fr(2, 3)]

print(statistics.mean(data))

See the following output.

➜  pyt python3 app.py
49/24
➜  pyt

#TypeError in Python

Let’s take string keys in Python dicitonary and try to get the mean of the string values. It will definitely get the error because we can not find the string’s mean value.

See the following code.

# app.py

import statistics

data = {"a": 11, "b": 21, "c": 19,
        "d": 29, "e": 18, "f": 46}

print(statistics.mean(data))

See the following output.

➜  pyt python3 app.py
Traceback (most recent call last):
  File "app.py", line 6, in 
    print(statistics.mean(data))
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/statistics.py", line 312, in mean
    T, total, count = _sum(data)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/statistics.py", line 148, in _sum
    for n,d in map(_exact_ratio, values):
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/statistics.py", line 230, in _exact_ratio
    raise TypeError(msg.format(type(x).__name__))
TypeError: can't convert type 'str' to numerator/denominator
➜  pyt

Finally, Python mean Example | Python Statistics mean() Function Tutorial is over.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.