The Python average of list can be done in many ways listed below:

Python Average by using the loop By using sum() and len() built-in average function in Python

Using mean() function to calculate the average from the statistics module. Using mean() from numpy library

Python Average via Loop
Using sum() and len() built-in functions
Using mean function from statistics module
Using mean() from numpy library

Method 1) Python Average via Loop

In this example, we have initialized the variable sum_num to zero and used for loop. The for-loop will loop through the elements present in the list, and each number is added and saved inside the sum_num variable. The average of list Python is calculated by using the sum_num divided by the count of the numbers in the list using len() built-in function.

Code Example:

Output:

Method 2) Python Average – Using sum() and len() built-in functions

In this example the sum() and len() built-in functions are used to find average in Python. It is a straight forward way to calculate the average as you don’t have to loop through the elements, and also, the code size is reduced. The average can be calculated with just one line of code as shown below.

Program Example:

Output:

Method 3) Using mean function from statistics module

You can easily calculate the “average” using the mean function from the statistics module. Example shown below Output:

Method 4) Using mean() from numpy library

Numpy library is commonly used library to work on large multi-dimensional arrays. It also has a large collection of mathematical functions to be used on arrays to perform various tasks. One important one is the mean() function that will give us the average for the list given.

Code Example:

Output:

Summary:

The formula to calculate average is done by calculating the sum of the numbers in the list divided by the count of numbers in the list. The average of a list can be done in many ways i.e

Python Average by using the loop By using sum() and len() built-in functions from python Using mean() function to calculate the average from the statistics module. Using mean() from numpy library