"""Find mean of a list of numbers."""
def average(nums):
"""Find mean of a list of numbers."""
return sum(nums) / len(nums)
def test_average():
"""
>>> test_average()
"""
assert 12.0 == average([3, 6, 9, 12, 15, 18, 21])
assert 20 == average([5, 10, 15, 20, 25, 30, 35])
assert 4.5 == average([1, 2, 3, 4, 5, 6, 7, 8])
if __name__ == "__main__":
"""Call average module to find mean of a specific list of numbers."""
print(average([2, 4, 6, 8, 20, 50, 70]))
Calculate the average of a list of numbers using mean.
Calculating the mean of a list of numbers is one of the most common ways to determine the average of those numbers.
Calculating a mean would be useful in these situations:
Given the list [2, 4, 6, 8, 20, 50, 70]
, let's calculate the average.
Send [2, 4, 6, 8, 20, 50, 70]
as input for a method/function.
Add all the numbers together.
2 + 4 + 6 + 8 + 20 + 50 + 70 = 160
, so sum = 160
.
Count the numbers in the list.
The list has seven numbers, so count = 7
.
Divide the sum of all the numbers by the count of the numbers.
sum = 160
count = 7
If we ignore significant digits: sum / count =
22.857142
If we properly consider significant digits: sum / count = 23
Return the value of 22.857142 or 23
.