How to Calculate the Average of Numbers in Python

In the day to activities of a programmer, Calculating the average for a set of numbers is one of the most common tasks. Python made it easy by creating inbuild functions to calculate the average of numbers easily. In this blog, We will learn to calculate the average of numbers with a few simple examples.

To calculate the average of numbers in Python, use the built-in function sum() to calculate the sum of all the numbers, and then divide the sum by the total number of numbers using the len() function.

Formula to calculate Average: sum of all the numbers / total number of numbers 
Calculate the Average of Numbers in Python

Steps to Calculate Average of Numbers

We are going to use two built-in functions in Python to calculate the average of numbers, Which is sum() and len()

sum(): It takes a list or other iterable object as an input and provides a sum of values

len(): It takes a list or other iterable object as input and provides the number of items in that iterable

Check this article to know more about len() function: How To Find the Length of a List in Python

Now, by using the above-discussed functions, We can easily calculate the average of the numbers with a simple 3-step process

Step1: We will start by adding all the numbers in the list, Which can be achieved using sum() function

sum(my_list)

Step2: Once the sum of all the numbers in the list is calculated, We will find the count of numbers in the list, Which can be achieved using len() function, Which will give you the length of the list

len(my_list)

Step3: At last, Using the average formula, We will divide the sum of numbers by the count of numbers to get the average value

Example Code:

# list of numbers 

numbers = [10, 21, 33, 42, 53] 

# sum of the above list of numbers 

total = sum(numbers) 

# calculate the average using the average formula

average = total / len(numbers) 

# print the result 

print("average:", average)

Output:

average: 31.8

Try it yourself -> Online Python editor to check the examples discussed here

Examples

We will discuss, Various other examples with different sets of input like float, negative number, strings, etc,

Example 1: Floating Point Numbers

# list of floating-point numbers 

numbers = [1.52, 23.5, 2.5, 23.5, 5.52] 

# sum of the float numbers 

total = sum(numbers) 

# calculate the average 

average = total / len(numbers) 

# print the result 

print("average:", average)

Output:

average: 11.308

In the above example, we have a list of floating-point numbers. With the same steps discussed above, We have calculated the average of a list of floating numbers.

Example 2: Negative Numbers

# list of negative numbers 

numbers = [-11, -23, -32, -4, -53] 

# sum of the negative numbers 

total = sum(numbers) 

# calculate the average 

average = total / len(numbers) 

# print the result 

print("average:", average)

Output:

average: -24.6

Try it yourself -> Online Python editor to check the examples discussed here

With the list of negative numbers. By using the same steps, We have found the average for negative numbers.

Example 3: Mixed List

#  list of mixed numbers 

numbers = [121, 2.5, -3, 4.22, -5.6] 

# sum of the numbers 

total = sum(numbers) 

# calculate the average 

average = total / len(numbers)

 # print the result 

print("The average is:", average)

Output:

The average is: 23.824

Try it yourself -> Online Python editor to check the examples discussed here

With the list of mixed numbers. With the same steps, we get a floating-point number as the average.

Example 4: Find an average for input numbers

To make it more interactive, We can get input numbers from the user in an interactive way (using the input function) and calculate the average.

# User Input

numbers = input("Enter numbers separated by spaces: ") 

# convert the input string to a list of numbers 

numbers = [float(x) for x in numbers.split()] 

# calculate the average 

total = sum(numbers) 

average = total / len(numbers) 

# print the result 

print("The average is:", average)

In this above example, we first use the input() function to ask the user for a set of numbers. The user should enter the numbers separated by spaces. Then we use a list comprehension to convert the input string to a list of floating-point numbers.

Once it is done, we used the same method as before to calculate the average of the numbers.

Output:

Enter numbers separated by spaces:  343 345 3345 434

The average is: 1116.75

Try it yourself -> Online Python editor to check the examples discussed here

This code will work for any number of inputs provided by the user, Only condition is to provide the number as a space-separated values

Possible ERRORS

If you try to use a list string as an input to sum() function, you will get the below error message, As the sum is an arithmetic operation

numbers = ["string1", "string2"]
TypeError: unsupported operand type(s) for +: 'int' and 'str'

Process finished with exit code 1

Conclusion

Calculating the average of numbers in Python is a simple task using the built-in sum() and len() functions. By following the steps outlined in this blog post, you should be able to calculate the average of any list of numbers in Python.

Good Luck with your Learning !!

Related Topics:

How to find the length of a string in Python (With wordcount example)

Python Find in List – How to Find the Index of an Item in a List (with example)

FizzBuzz Interview Question Python

Similar Posts