Average of N Numbers in Python

We will develop a program on how to find the average of n numbers in Python. The average is defined as the mean value which is equal to the ratio of the sum of the number of a given set of values to the total number of values present in the set. Here, we will calculate the average of n numbers using various methods.

How to find the average of numbers:

Average Formula = Total sum of all numbers / Number of item in the set
Average = (x1+x2+x3+…+xn)/n

Mathematically,

Inputs: a=2, b=4, c=8, d=2
Average = (a+b+c+d)/4 = (2+4+8+2)/4 = 16/4 = 4

Now let’s see how we can calculate average of n numbers in Python.

Average in Python using For Loop

This Python program is the simplest and easiest way to calculate the average of N number. First, we defined the total number we want to enter in inputs. Then, we will take numbers and calculate the total sum of those numbers using the For Loop. Finally, calculate the average of those numbers using a formula and print the average value.

# Python program to find the average of n numbers

# total number you want to enter
n = int(input('How many numbers: '))

# denotes total sum of n numbers
total_sum = 0

for i in range (n):
    # take inputs
    num = float(input('Enter number: '))
    # calculate total sum of numbers
    total_sum += num

# calculate average of numbers
avg = total_sum / n

# print average value
print('The average value of numbers = %0.2f' %avg)

Output for the different input values:-

How many numbers: 3
Enter number: 10
Enter number: 15
Enter number: 12
The average value of numbers = 12.33

How many numbers: 5
Enter number: 23
Enter number: 20
Enter number: 34
Enter number: 50
Enter number: 29
The average value of numbers = 31.20

Python Program to Find Average of N Numbers using While Loop

In the previous program, we used for loop to calculate average but in this program, we are using the While Loop to find the average of n numbers.

# Python program to find the average of n numbers

# total number you want to enter
n = float(input('How many numbers: '))

total_sum = 0

i =1
while i <= n:
    num = float(input('Enter number: '))
    total_sum += num
    
    i = i+1

# calculate average of numbers
avg = total_sum / n

# print average value
print('The average of numbers = %0.2f' %avg)

Output:-

How many numbers: 2
Enter number: 10
Enter number: 20
The average of numbers = 15.00

Program to Find Average of N Numbers in Python

This python program also performs the same task but with different methods. In this program, we can also take the help of a function to find the average of n numbers in python. A function is a block of code that performs a specific task.

# Python program to find the average of n numbers

def avg(arr, x):  #user-defined function
    sum = 0
    for i in range(n):
        # calculate total sum of numbers
        sum = sum+arr[i]
    # calculate average of numbers
    avg = sum/x
    return avg

# total number you want to enter
n = int(input('How many numbers: '))

# take inputs
num = []
for i in range(n):
    num.append(float(input('Enter number: ')))

# calling function and print average value
print('The average value of numbers = %0.2f' %avg(num, n))

Output:-

How many numbers: 4
Enter number: 12
Enter number: 20.5
Enter number: 46
Enter number: 35.39
The average value of numbers = 28.47

Also See:- How to find the average of 3 numbers in Python

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *