How to Get the Average of a List of Numbers in Python
To calculate the average (middle) of a list of numbers divide the sum of them by the length of the list. In this tutorial, we will go through how to get the average in Python.
Python Average Example
To get the average in Python, first create a list of numbers. Then get their sum using the sum() function, divide that by the list count returned from the len() function:
numbers = [23, 4, 53, 9, 10] res = sum(numbers) / len(numbers) print(res)
Get Average in Python Using the Statistics Package
A cleaner way to get averages in Python is by using the statistics package:
import statistics numbers = [23, 4, 53, 9, 10] statistics.mean(numbers) print(res)