Algorithm to Calculate the average of numbers in a List

Find average of a list in python

Prerequisites: sum() function, len() function, round() function, reduce(), lambda, and mean().

Given a list of numbers, the task is to find average of that list. Average is the sum of elements divided by the number of elements.

Examples:

Input : [4, 5, 1, 2, 9, 7, 10, 8] Output : Average of the list = 5.75 Explanation: Sum of the elements is 4+5+1+2+9+7+10+8 = 46 and total number of elements is 8. So average is 46 / 8 = 5.75 Input : [15, 9, 55, 41, 35, 20, 62, 49] Output : Average of the list = 35.75 Explanation: Sum of the elements is 15+9+55+41+35+20+62+49 = 286 and total number of elements is 8. So average is 46 / 8 = 35.75

1. Algorithm for calculating the average

In simple words, to calculate the average of N numbers we have to add all the numbers, and then divide their sum by N.

In pseudo-code, we can list the steps needed in the following manner:

Step 1 : Start Step 2 : sum = 0, i = 0, average = 0, count = 0 Step 3 : i = i + 2 Step 4 : sum = sum + i, count = count + 1 Step 5 : if i <= 10 then go to on step 3, else go to on step 6 Step 6 : average = sum/count Step 7 : Display "average" Step 8 : Stop

Find average of a list in python?

PythonProgrammingServer Side Programming

Python provides sum function for calculating n number of element. Here we use this function and then calculate average.

Python Average

The Python Average function is used to find the average of given numbers in a list. The formula to calculate average in Python is done by calculating the sum of the numbers in the list divided by the count of numbers in the list.

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

In this Python tutorial, you will learn how to calculate average in Python:

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

How to take the average of a list in Python

The goal here is to find the average/mean of a list of numbers. The average is the sum of all the numbers in a list divided by its length.

Algorithm to Calculate the average of numbers in a List

Algorithm and Flowchart to Compute Average of Three Numbers

[41968 views]


What is Average?

Average is a mathematical term used in taking the appropriate number among the list of numbers. Formula for Average = Sum of all values / No. of values in the list.

What is Average used for?

There are various scenarios in which we have to say a single value of things. For that instance we use average number for single value. For Eg. If in a batsman scores 20 runs in 1st match, 37 runs in 2nd match and 78 runs in third match. So we can say Average of the batsman is (20+37+78)/3=45. It says that the batsman can score around 45 runs in a match. Here, we can see that we are converting multiple values into single value to make things easier.

So in Conclusion I will be reading three numbers and finding average of them by adding the numbers and then dividing the sum with 3.

Python Average

There are two methods employed to find the average of a list of numbers in Python:

  • By calculating the sum of the list and then dividing that number by the length of the list. The length of the list is how many values are in the list. or;
  • By using the statistics.mean() method.

While both methods return the average of a list of numbers, there are different factors you should consider when choosing which one to use.

In this tutorial, we discuss how to use the aforementioned approaches to find the average of a list in Python. We’ll walk through two examples to help you get started.