DEV Community

johnnyhopkins28160@gmail.com
johnnyhopkins28160@gmail.com

Posted on

Calculating the Mean (Correction)

Here is the program:

Programmer: Johnny L. Hopkins

Date: April 20, 2021

mean.py: Computing the aritmetic mean without using libraries

We will use matplotlib for basic visualizations based on the mean

import matplotlib.pyplot as plt

We need sample data to calculate the mean

We do this by declaring a new list

list_values = {80, 75, 93, 88, 99, 100, 80}

We will now build the equation for the mean by using

sum(), which takes the iterables in our list and sum over the

elements. To count the elements we will use len(). We can combine

the sum() and len() to build the equation for the mean. We declare a

new variable to store the value of the mean.

values_mean = sum(list_values) / len(list_values)

We now will print out the mean to the standard output:

print("The mean is: ", values_mean)

Finally, we will graph a histogram to see the distribution of values

To output the histogram, we have to specify the list as the values

to be graphed. If we don't specify a variable, an error is returned to

standard output

plt.hist(list_values, density = 1)
plt.show()

Here is the output:

Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.

Try the new cross-platform PowerShell https://aka.ms/pscore6

PS C:\Users\hopki> & python c:/Users/hopki/OneDrive/Documents/mean.py
The mean is: 89.16666666666667
PS C:\Users\hopki>

This comes directly from Visual Studio Code.

Top comments (0)