Python for Beginners: Calculate Average in Python

We have to perform many mathematical calculations in a python program to process any data. In this article, we will look at different ways to calculate the average of given numbers in python. 

How to calculate the average of given numbers

The average of given numbers is defined as the sum of all the numbers divided by the total count of the numbers. 

For example, if we are given numbers 1, 2, 4, 5, 6, 7, 8, 10, and 12, we can calculate the average of the numbers by first calculating their sum and then dividing the sum by total count of numbers. Here, the sum of all the given numbers is 55 and their total count is 9. So, the average of all the numbers will be 55/9 i.e. 6.111 .

image

Calculate average using for loop in Python

If we are given a list of numbers, we can calculate the average using the for loop. First, we will declare a sumofNums and a count variable and initialize them to 0. Then, we will traverse each element of the list. While traversing, we will add each element to the sumofNums variable. At the same time, we will also increment the count variable by 1. After traversing the whole list, we will have the sum of all the elements of the list in the sumofNums variable and the total number of elements in the count variable. Now, we can divide the sumofNums by count to obtain the average of the elements of the list as follows.

numbers = [1, 2, 34, 56, 7, 23, 23, 12, 1, 2, 3, 34, 56]
sumOfNums = 0
count = 0
for number in numbers:
    sumOfNums += number
    count += 1
average = sumOfNums / count
print("The list of numbers is:", numbers)
print("The average of all the numbers is:", average)

Output:

The list of numbers is: [1, 2, 34, 56, 7, 23, 23, 12, 1, 2, 3, 34, 56]
The average of all the numbers is: 19.53846153846154

Calculate the average using built-in functions

Instead of using for loops, we can use built-in functions in python to calculate the average of elements in a given list.

We can calculate the sum of all the elements of the list using the sum() method and then we can calculate the total number of elements in the list using the len() method. In this way, we will have the sum of the numbers and the total count of the numbers with which we can calculate the average as follows.

numbers = [1, 2, 34, 56, 7, 23, 23, 12, 1, 2, 3, 34, 56]
sumOfNums = sum(numbers)
count = len(numbers)
average = sumOfNums / count
print("The list of numbers is:", numbers)
print("The average of all the numbers is:", average)

Output:

The list of numbers is: [1, 2, 34, 56, 7, 23, 23, 12, 1, 2, 3, 34, 56]
The average of all the numbers is: 19.53846153846154

Alternatively, we can use the mean() method of the statistics module to directly calculate the average of the elements of the list. We will pass the given list of numbers as input to the mean() method and it will return the average of numbers as shown in the following example.

import statistics
numbers = [1, 2, 34, 56, 7, 23, 23, 12, 1, 2, 3, 34, 56]
average = statistics.mean(numbers)
print("The list of numbers is:", numbers)
print("The average of all the numbers is:", average)

Output:

The list of numbers is: [1, 2, 34, 56, 7, 23, 23, 12, 1, 2, 3, 34, 56]
The average of all the numbers is: 19.53846153846154

Conclusion

In this article, we have discussed different ways to calculate the average of given numbers in Python. You can read about other operations in the article on python operators.

Recommended Python Training

Course: Python 3 For Beginners

Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.

Author: Shantun Parmar

Leave a Reply

Your email address will not be published.