In this tutorial, we are going to learn writing python program to find the average of numbers given as input from the user.
In Python, there are several ways to compute the average, depending on the source and structure of the input data. Each method has its benefits and is suitable for different scenarios. Here, we’ll explore various ways to calculate the average of numbers using Python, illustrating the flexibility and power of Python for tasks involving numerical computation.
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.
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.
Example
For input number [10, 20, 30, 40, 50] Average = ( 10+20+30+40+50 )/ 5 The output generated is 30.0.
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
Program 1: Using a Simple Loop and Input from the User
size=int(input("Give the number of elements you want in array: "))
arr=[]
#taking input of the list
for i in range(0,size):
elem=int(input("Enter the number "+str(i +1)+": "))
arr.append(elem)
#taking average of the elements of the list
avg=sum(arr)/size
print("Average of the array elements is ",avg)
Output :
Give the number of elements you want in array: 5 Enter the number 1: 10 Enter the number 2: 20 Enter the number 3: 30 Enter the number 4: 40 Enter the number 5: 50 Average of the array elements is 30.0
Explanation:
In this example, the user inputs the size of list 5 and then the entries of the list as [10, 20, 30, 40, 50], the average calculated as
Average = ( 10+20+30+40+50 )/ 5
The output generated is 30.0.
The above Python program calculates the average of a list of numbers entered by the user. First, it asks the user how many numbers (elements) they want to add to the list. It then uses a loop to get each number from the user and adds these numbers to a list. After all numbers have been entered, the program calculates the average by adding all the numbers in the list together and dividing by the total number of numbers. Finally, it displays the average to the user. The program is straightforward and involves common tasks like taking input, using loops, and performing arithmetic operations, making it ideal for beginners learning how to manipulate lists and perform basic mathematical calculations in Python.
Program 2: Using the Built-in sum() and len() Functions
For lists or arrays of numbers, Python’s built-in sum()
and len()
functions provide a quick and straightforward way to calculate the average.
def calculate_average(numbers):
return sum(numbers) / len(numbers)
numbers = [10, 20, 30, 40, 50]
print("Average of the number is :", calculate_average(numbers))
Output
Average of the number is : 30.0
This Python program defines a function called calculate_average
that calculates the average of a list of numbers. The function takes a single parameter, numbers
, which is expected to be a list of numerical values. Inside the function, the average is computed using the built-in sum()
function to get the total sum of the numbers in the list, and len()
to find out how many numbers are in the list. The average is then calculated by dividing the total sum by the number of elements. The function returns this average value.
Below the function definition, a list of numbers is defined as [10, 20, 30, 40, 50]
. The program then calls the calculate_average
function with this list as the argument, and prints the resulting average. The output will show that the average of the numbers in the list is 30.0. This program efficiently demonstrates how to encapsulate functionality in a function for reuse and clarity, making it a clean and effective example of basic Python programming for numerical computations.