lettura simple

Arithmetic Mean in Python

In Python, the mean() function is a powerful tool for computing the arithmetic mean of a list of numbers.

mean(list)

Simply insert the list of numbers within the parentheses, and the function will return the arithmetic mean of those numbers.

This function is part of the "statistics" module, so you must import it before use. Alternatively, you may choose to import the mean() function from the "numpy" or "pandas" modules. Note that an empty list will result in a StatisticsError being raised.

Here's a practical example.

First, assign a list of numbers to the variable "numbers".

numbers = [1, 2, 3, 4, 5]

Next, import the mean() function from the statistics module with the following line.

from statistics import mean

This only needs to be done once at the beginning of your session or program.

Now you can use the mean() function to calculate the average of the numbers in the list.

mean = statistics.mean(numbers)

This will calculate the mean of the numbers in the list [1, 2, 3, 4, 5].

To display the result, use:

print(mean)

The mean of the list of numbers is equal to 3.

3

The statistics.mean() function works by summing all the numbers in the list and then dividing by the total number of elements.

In the example provided, the arithmetic mean is calculated as follows:

$$ mean = \frac{1+2+3+4+5}{5} = \frac{15}{5} = 3 $$

With this understanding, you are now equipped to compute the arithmetic mean using Python.




Report a mistake or post a question




FacebookTwitterLinkedinLinkedin