In this, you'll get to learn how to find the average of n numbers in python (using different approaches) where the value of n is entered by a user at the runtime with n no. of numbers. i.e. Let's say if the user entered 4, the prompt should ask to enter 4 numbers. If the user enters 4 numbers -- let's say 5, 10, 15, 20 then the output should be 50
((5+10+15+20)/4=12.5).
This covers four different approaches:
This is an example to find the sum of n numbers using for loop. Where the value of n and then n numbers must be entered by the user at the runtime. If the user enters the value of n as 5, then this block of code must be executed 5 times to get 5 numbers. After 5 numbers are received all numbers are added and initialized to sum, one by one. Then total sum value is divided by n to get average of the numbers.
sum = 0
n = int(input("Enter the Value of n: "))
print(f"Enter {n} numbers (type a number and press ENTER): ")
for i in range(n):
num = int(input())
sum = sum+num
avg = sum/n
print(f"The average of {n} numbers = {avg}")
Here, the f-string is a string literal that is used as an f at the beginning and curly braces containing expression that will be replaced with its value.
Output:
Enter the Value of n: 4
Enter 4 numbers (type a number and press ENTER):
5
10
15
20
The average of 4 numbers = 12.5
This example is to find the average of numbers that is the same as above but using while loop instead.
sum = 0
n = int(input("Enter the Value of n: "))
print(f"Enter {n} numbers (type a number and press ENTER): ")
i = 0
while i < n:
num = int(input())
sum = sum+num
i += 1
avg = sum/n
print(f"The average of {n} numbers = {avg}")
Output:
Enter the Value of n: 4
Enter 4 numbers (type a number and press ENTER):
5
10
15
20
The average of 4 numbers = 12.5
This also does the same task as the above programs but using the function method.
def avgOf(n_list, N):
result = 0
for i in range(N):
result += n_list[i]
avg = result/N
return avg
num_list = []
n = int(input("Enter the Value of n: "))
print(f"Enter {n} numbers (type a number and press ENTER): ")
for i in range(n):
num_list.insert(i, int(input()))
sum = avgOf(num_list, n)
print(f"The sum of {n} numbers = {sum}")
Here, the function avgOf()
takes two arguments -- number list and the value of n, then it executes using for loop to find the sum of individual items in the list. The sum of all the numbers in the list is stored in result
variable which is then divided by n to get the desired result (average of n numbers entered by the user).
Output:
Enter the Value of n: 4
Enter 4 numbers (type a number and press ENTER):
5
10
15
20
The average of 4 numbers = 12.5
This also does the same task as the above programs but using the function method.
class averageCalculator:
def avgOf(self, n_list, N):
result = 0
for i in range(N):
result += n_list[i]
avg = result/N
return avg
n = int(input("Enter the Value of n: "))
num_list = []
print(f"Enter {n} numbers (type a number and press ENTER): ")
for i in range(n):
num_list.insert(i, int(input()))
# Function call to get average of n numbers
avg1 = averageCalculator()
res = avg1.avgOf(num_list, n)
print(f"The Sum of {n} natural numbers = {res}")
Here, All properties of the class named averageCalculator get assigned to an object named avg1. Then the object can be used to access the member function of the class averageCalculator using the dot (.) operator. (avg1.avgOf(num_list, n))
Output:
Enter the Value of n: 4
Enter 4 numbers (type a number and press ENTER):
5
10
15
20
The Sum of 4 natural numbers = 12.5
In this, we discussed different examples for different methods to find the average of n numbers in Python -- using for loop
, using while loop
, using a function
, and using class
.