AppDividend
Latest Code Tutorials

Python mean List | How To Calculate Average In Python

0

Python mean() is an inbuilt statistics module function that used to calculate average of numbers and list. The 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. Python is a popular language when it comes to data analysis and statistics.

Python 3 provides the statistics module, which comes with very useful functions like mean(), median()mode(), etc.

The arithmetic mean is a 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 that vary in range.

Python mean() Function

How to calculate average of numbers? Well, the answer is the mean() method.

The arithmetic mean is the sum of data divided by the number of data-points. It is a measure of the central location of data in a set of values that vary in range.

In Python, we usually do this by dividing the sum of given numbers with the count of the number present.

Python 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 average of list

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

#How To Calculate Mean of a tuple In Python

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.

#How To Calculate Mean of a Dictionary in Python

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

#How To 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

#How To 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

#How To 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

#How to calculate average of list in Python

See the following code.

# app.py

import statistics

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

See the output.

➜  pyt python3 app.py
18.53846153846154
➜  pyt

#TypeError in Python

Let’s take string keys in the Python dictionary 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 Statistics mean() Function Example is over.

Recommended Posts

The sum() Function

The pow() Function

Python Int Function Example

The float() Example

Leave A Reply

Your email address will not be published.

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