Python program to find the average of numbers

python program to find average

In this tutorial, we are going to learn a python program to find the average of numbers given as input from the user.

Problem Statement

For any sequence of numbers that are input by the user, we have to calculate the average of the input sequence numbers.

For example:

Case1: If the user input the sequence 1,2,3,4,5,6,7,8,9.

                        then the output should be 5.

Case2: If the user input the sequence 11,22,33,44,55,66,77,88,99.

                        then the output should be 55.

‘for’ loop statement

The ‘for loop’ is a looping statement in python which is used to iterate over a sequence or any other iterable objects such as lists, tuples, dictionaries, strings, etc.

Flow Chart for ‘for loop’

Range() function

It returns the sequence of a given number between a given range. Range() function is a Python built-in function that is used when the user needs to perform an action-specific number of times. It is commonly used with looping statements.

It takes 3 arguments

  1. start: it is theinteger starting from which the series of integers is to be returned.
  2. stop: it is the integer earlier than which the collection of integers is to be lower back.
  3. step: it is the integer that determines the increment among each integer within the collection.

append() function

append() is a built-in function in python that helps to add a new item to the end of the list. It does not make the new list but instead modifies the list.

Sum() function

Sum() is a Python built-in function that returns the arithmetic sum of the elements of the iterable objects provided the elements of the iterable objects are real numbers. It takes the iterable objects as its parameter.

What is Average of numbers?

Average also called the arithmetic mean, is calculated by adding a group or collection of numbers and then dividing that sum with the number of elements of the same group or collection.

Algorithm to find the average of numbers

Step 1: Start

Step 2: take input n from the user for fixing the size of the list.

Step 3: Create an empty list

Step 4: Add items to the list

Step 5: assign a variable, avg as

                     Avg = sum(list)/n

Step 6: print avg

Step 7: Stop

Python code to find the average of numbers

Output 1:

average program in python

Explanation:

In this example, the user inputs the size of list 5 and then the entries of the list as   [9, 5, 7, 88, 21],  the average calculated as

                                    Average =    ( 9+5+7+88+21 )/ 5

The output generated is 26.0.

Output 2:

average program in python

Explanation:

In this example, the user inputs the size of list 6 and then the entries of the list as   [51, 11, 52, 151, 101,21],  the average calculated as

                                    Average =    ( 51+11+52+151+101+21 )/ 6

The output generated is 54.5.