How to Find the Average of a List in Python

In this tutorial, we will learn how to program "How to Find the Average of a List in Python." The objective is to efficiently calculate the average of a list based on the given numbers. This tutorial will guide you step by step through the entire process of properly computing the average value. By the end of this tutorial, you will have a solid understanding of how to implement this task effectively in Python, helping you strengthen your problem-solving abilities and enhance your coding skills.

This topic is straightforward to understand. Just follow the instructions provided, and you will be able to complete it with ease. The program demonstrated will guide you step by step through the process of sorting a hyphen-separated sequence of words in alphabetical order. So, let’s dive into the coding process!

Getting Started:

First you will have to download & install the Python IDLE's, here's the link for the Integrated Development And Learning Environment for Python https://www.python.org/downloads/.

Creating Main Function

This is the main function of the application. The following code will display a simple GUI in terminal console that will display program. To do this, simply copy and paste these blocks of code into the IDLE text editor.
  1. while True:
  2.     print("\n============= Find the Average of a List =============\n")
  3.  
  4.     n=int(input("Enter a number: "))
  5.     a=[]
  6.     for i in range(0,n):
  7.         elem=int(input("Enter element: "))
  8.         a.append(elem)
  9.     avg=sum(a)/n
  10.     print("Result",round(avg,2))
  11.  
  12.    
  13.     opt = input("\nDo you want to try again?(yes/no): ")
  14.            
  15.     if opt.lower() == 'yes':
  16.         ret=False
  17.     elif opt.lower() == 'no':
  18.         ret=True
  19.         print("Exiting program....")
  20.     else:
  21.         print("Please enter yes/no:")
  22.         break
  23.  
  24.     if ret == False:
  25.         continue

This Python program calculates the average of a list of user-provided numbers. It repeatedly prompts the user to enter how many numbers they want to include, collects those numbers in a list, computes the average using the sum() function divided by the count, and prints the result rounded to two decimal places. Afterward, the user can choose to run the program again or exit.

Output:

There you have it we successfully created How to Find the Average of a List in Python. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

More Tutorials for Python Language

Python Tutorials