Calculate sum and average of a given list of numbers in Python. Finding the sum and average of a list of numbers is a fundamental task in programming and data analysis. In this article, you’ll learn several approachable methods using concise built-in tools, along with runnable examples and notes about edge cases and complexity.
sum() and len()This is the simplest and most Pythonic approach. It uses built-in functions to compute the sum and average efficiently with clean, readable code.
# Example: compute sum and average using built-in functions
numbers = [10, 20, 30, 40, 50]
# compute sum
total = sum(numbers)
# compute average
average = total / len(numbers)
print("Sum :", total)
print("Average :", average)
Code language: PHP (php)
Output
Sum : 150
Average : 30.0
Code language: CSS (css)
Explaination: This program calculates the total using Python’s built-in sum() function and then computes the average by dividing the total by the number of elements using len(). It is efficient, readable, and the most Pythonic way to perform this task. Make sure the list is not empty to avoid division by zero errors.
Users can specify how many numbers they want to enter.
The program then collects each value from the user.
# interactive version
numbers = []
n = int(input("Enter number of elements: "))
for i in range(n):
x = float(input(f"Enter element {i+1}: "))
numbers.append(x)
if len(numbers) == 0:
print("No numbers were entered.")
else:
total = sum(numbers)
average = total / len(numbers)
print("Sum :", total)
print("Average :", average)
Code language: PHP (php)
Explaination:
This program first asks the user how many numbers to enter and stores each value in a list. It then calculates the total using sum() and finds the average by dividing by the list length. The empty-list check prevents division by zero and ensures safe execution.
If you want to avoid built-ins or demonstrate manual summation,
this loop-based approach shows how the total is calculated step by step:
numbers = [5, 15, 25, 35]
total = 0
for num in numbers:
total += num
average = total / len(numbers) if numbers else 0
print("Sum :", total)
print("Average :", average
)
Code language: PHP (php)
Explaination:
Here, we iterate through each element of the list and keep adding the values to calculate the total. After finding the total sum, the average is computed by dividing it by the length of the list, with a safety check to handle an empty list.
statistics or numpy If you are working with numerical data using statistics or numpy,
these libraries provide accurate and convenient helper functions:
# Using statistics (part of standard library)
import statistics
numbers = [1.5, 2.5, 3.0]
total = statistics.fsum(numbers) # precise floating-point summation
average = statistics.mean(numbers)
# Using numpy (popular for arrays & large datasets)
import numpy as np
arr = np.array([1.5, 2.5, 3.0])
total_np = np.sum(arr)
avg_np = np.mean(arr)
Code language: PHP (php)
Explaination:
Here, the statistics module provides precise functions like fsum() and mean() to compute the total and average. Similarly, NumPy offers fast vectorized functions np.sum() and np.mean() that work efficiently for large numerical datasets.
len(numbers) == 0 before dividing; otherwise you’ll get ZeroDivisionError.float()), or handle TypeError.math.fsum() or statistics.fsum() for more accurate floating-point summation.sum() or a loop).total and average use constant space.