How to Calculate the Average of a Given List in Python


In this example we will show how to calculate the average of a list number. In Python, it can be completed through methods of loop statements and basic calculations.

Source Code

#! /usr/bin/env python3
# -*- coding: utf-8 -*-

num = int(input("Please input the number of digits: "))
list_n = []
for i in range(0, num):
    n = int(input("Input the digit: "))
    list_n.append(n)
avg = sum(list_n) / num
print("Average is {:.2f}".format(avg))

Output:

Please input the number of digits: 4
Input the digit: 10
Input the digit: 15
Input the digit: 12
Input the digit: 16
Average is 13.25
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments