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.
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.
Similar Reads
-
Find middle element of a linked list in single pass
Linked lists are a fundamental data structure used in computer science for organizing and managing data. A linked list is… -
Python Program to Find Last 3rd element in Singly Linked List
In this tutorial, we are going to learn the writing python program to Find 3rd element of Linked List from… -
Most important JavaScript Interview Questions To Prepare
In this Page we have collected and explained Most important Javascript Interview Questions and Answers for begineers, freshers as well… -
Sum of digits of Given Number in Java
In this tutorial we will learn writing Java program to calculate the sum of its digit. We will also see… -
Hibernate Interview Questions for 2+ years of experience
Certainly! Here's a list of commonly asked interview questions on Hibernate for candidates with 2+ years of experience: Basic Hibernate… -
68 Most Important Microservices Interview Questions
Certainly, here's an extended list of 50 commonly asked interview questions on microservices for candidates with 2+ years of experience:… -
60 Most Important Git Interview Questions
Certainly! Here is a list of commonly asked interview questions on Git for candidates with fresher or having of experience… -
50+ Most important Java Interview Questions for 5+ Years Exp
1. Explain the SOLID principles in Java. Provide examples of how you have applied these principles in your projects. SOLID… -
60+ Spring Boot interview questions for 4+ years Exp.
1. What is Spring Boot and how does it differ from the Spring framework? Spring Boot is a framework designed… -
60+ Mostly Asked Spring Boot Interview Questions for 3+ Yrs
Here is a list of 60+ Spring Boot interview questions for candidates with 3+ years of experience: 1. What is…