• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
PythonForBeginners.com

PythonForBeginners.com

Learn By Example

  • Home
  • Learn Python
    • Python Tutorial
  • Categories
    • Basics
    • Lists
    • Dictionary
    • Code Snippets
    • Comments
    • Modules
    • API
    • Beautiful Soup
    • Cheatsheet
    • Games
    • Loops
  • Python Courses
    • Python 3 For Beginners
You are here: Home / Basics / Calculate Average in Python

Calculate Average in Python

Author: Aditya Raj
Last Updated: December 16, 2021

We have to perform many mathematical calculations in a python program to process any data. In this article, we will look at different ways to calculate the average of given numbers in python. 

How to calculate the average of given numbers

The average of given numbers is defined as the sum of all the numbers divided by the total count of the numbers. 

For example, if we are given numbers 1, 2, 4, 5, 6, 7, 8, 10, and 12, we can calculate the average of the numbers by first calculating their sum and then dividing the sum by total count of numbers. Here, the sum of all the given numbers is 55 and their total count is 9. So, the average of all the numbers will be 55/9 i.e. 6.111 .

Calculate average using for loop in Python

If we are given a list of numbers, we can calculate the average using the for loop. First, we will declare a sumofNums and a count variable and initialize them to 0. Then, we will traverse each element of the list. While traversing, we will add each element to the sumofNums variable. At the same time, we will also increment the count variable by 1. After traversing the whole list, we will have the sum of all the elements of the list in the sumofNums variable and the total number of elements in the count variable. Now, we can divide the sumofNums by count to obtain the average of the elements of the list as follows.

numbers = [1, 2, 34, 56, 7, 23, 23, 12, 1, 2, 3, 34, 56]
sumOfNums = 0
count = 0
for number in numbers:
    sumOfNums += number
    count += 1
average = sumOfNums / count
print("The list of numbers is:", numbers)
print("The average of all the numbers is:", average)

Output:

The list of numbers is: [1, 2, 34, 56, 7, 23, 23, 12, 1, 2, 3, 34, 56]
The average of all the numbers is: 19.53846153846154

Calculate the average using built-in functions

Instead of using for loops, we can use built-in functions in python to calculate the average of elements in a given list.

We can calculate the sum of all the elements of the list using the sum() method and then we can calculate the total number of elements in the list using the len() method. In this way, we will have the sum of the numbers and the total count of the numbers with which we can calculate the average as follows.

numbers = [1, 2, 34, 56, 7, 23, 23, 12, 1, 2, 3, 34, 56]
sumOfNums = sum(numbers)
count = len(numbers)
average = sumOfNums / count
print("The list of numbers is:", numbers)
print("The average of all the numbers is:", average)

Output:

The list of numbers is: [1, 2, 34, 56, 7, 23, 23, 12, 1, 2, 3, 34, 56]
The average of all the numbers is: 19.53846153846154

Alternatively, we can use the mean() method of the statistics module to directly calculate the average of the elements of the list. We will pass the given list of numbers as input to the mean() method and it will return the average of numbers as shown in the following example.

import statistics
numbers = [1, 2, 34, 56, 7, 23, 23, 12, 1, 2, 3, 34, 56]
average = statistics.mean(numbers)
print("The list of numbers is:", numbers)
print("The average of all the numbers is:", average)

Output:

The list of numbers is: [1, 2, 34, 56, 7, 23, 23, 12, 1, 2, 3, 34, 56]
The average of all the numbers is: 19.53846153846154

Conclusion

In this article, we have discussed different ways to calculate the average of given numbers in Python. You can read about other operations in the article on python operators.

Related

Recommended Python Training

Course: Python 3 For Beginners

Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.

Enroll Now

Filed Under: Basics Author: Aditya Raj

More Python Topics

API Argv Basics Beautiful Soup bitly Cheatsheet Code Code Snippets Command Line Comments Concatenation crawler Data Structures Data Types deque Development Dictionary Dictionary Data Structure In Python Error Handling Exceptions Filehandling Files Functions Games GUI Json Lists Loops Mechanzie Modules Modules In Python Mysql OS pip Python Python On The Web Python Strings Queue Requests Scraping Scripts Split Strings System & OS urllib2

Primary Sidebar

Menu

  • Basics
  • Cheatsheet
  • Code Snippets
  • Development
  • Dictionary
  • Error Handling
  • Lists
  • Loops
  • Modules
  • Scripts
  • Strings
  • System & OS
  • Web

Get Our Free Guide To Learning Python

Most Popular Content

  • Reading and Writing Files in Python
  • Python Dictionary – How To Create Dictionaries In Python
  • How to use Split in Python
  • Python String Concatenation and Formatting
  • List Comprehensions in Python
  • How to Use sys.argv in Python?
  • How to use comments in Python
  • Try and Except in Python

Recent Posts

  • Pandas Append Row to DataFrame
  • Convert String to DataFrame in Python
  • Pandas DataFrame to List in Python
  • Solved: Dataframe Constructor Not Properly Called Error in Pandas
  • Overwrite a File in Python

Copyright © 2012–2023 · PythonForBeginners.com

  • Home
  • Contact Us
  • Privacy Policy
  • Write For Us