The average (arithmetic mean) of N numbers is their sum divided by the count.
Remember
average = (a₁ + a₂ + … + aₙ) / N
Example: 1, 2, 3, 4, 5
sum = 15, N = 5
average = 15 / 5 = 3
Values
Sum
N
Average
1, 2, 3, 4, 5
15
5
3
10, 15, 20, 25, 30
100
5
20
2, 4
6
2
3
1, 2
3
2
1.5
Approach
How to Find the Average of N Numbers in Python
Sum every value once, then divide by the count — guard empty input first.
Reject N <= 0 or an empty list (no division by zero).
Iterate the N values (loop, sum, or statistics.mean).
Accumulate a running sum.
Return sum / N (Python 3 / keeps a float).
Method
Idea
Time
Best for
Running sum + N
Loop N times with a fixed list
O(N)
Interviews, demos
List helper
Sum len(values) items
O(N)
Reusable default
sum / statistics.mean
Built-in one-liners
O(N)
Concise scripts
Pseudocode
Pseudocode
function Average(values):
n = length of values
if n == 0:
return error // or a documented sentinel
sum = 0
for each value in values:
sum = sum + value
return sum / n
Reference
Cheat Sheet
Keep these facts handy while writing the program.
Topic
Detail
Formula
average = sum / N
Guard
Never divide when N <= 0
Python 3
/ keeps decimals (// truncates)
Not median
Median needs sorting; average does not
1..5
sum 15 → average 3
Try it
Live Preview
Enter comma-separated numbers to see the sum, count, and average.
Comma-separated numbers. Tap a chip or type your own list.
Result
values = [1, 2, 3, 4, 5]
sum = 15, N = 5
average = 3
Start with a running-sum helper and fixed N. Then average any list by length, and finish with sum / statistics.mean.
Example 1 — Running Sum with Fixed N
Validate N, loop N times over a values list, keep a running total, then divide.
Python
def average_of_n_numbers(n, values):
if n <= 0:
return 0
total = 0
for i in range(n):
total += values[i]
return total / n
n = 5
values = [1, 2, 3, 4, 5]
result = average_of_n_numbers(n, values)
print(f"The average of the entered numbers is: {result}")
Output
The average of the entered numbers is: 3.0
How It Works
Return 0 if n <= 0 (documented sentinel).
Add values[0] through values[n - 1] into total.
Divide once after the loop: total / n.
Python 3 / keeps decimals, so 1 / 2 is 0.5.
Sample 1…5 prints 3.0.
Example 2 — Average from a List
Reusable helper: sum every element, then divide by len.
Python
def average_from_list(values):
if not values:
return 0
total = 0
for x in values:
total += x
return total / len(values)
data = [10, 15, 20, 25, 30]
avg = average_from_list(data)
print(f"Average = {avg}")
Output
Average = 20.0
How It Works
Reject an empty list before dividing.
Sum every element in one forward pass.
Divide by len(values) for the mean.
10+15+20+25+30 = 100 → average 20.0.
Prefer this shape in interviews: one clear helper.
Example 3 — Mean with sum / statistics.mean
Same formula with built-ins — still guard empty lists.
Python
from statistics import mean
def average(values):
if not values:
return 0
return sum(values) / len(values)
data = [1, 2, 3, 4, 5]
print(average(data))
print(mean(data))
Output
3.0
3
How It Works
Return early when the list is empty.
sum(values) adds every element; divide by len(values).
statistics.mean does the same job and raises on an empty sequence.
Same math as the for loop — just more compact.
Sample 1…5 prints 3.0 and 3 (mean returns an int when exact).
Edge Cases & Pitfalls
Check these before calling the solution done.
Empty
Never divide by N = 0
Empty lists are undefined. Return an error or a documented sentinel.
N = 1
Average = itself
A single value’s average is that value.
Negatives
Allowed
The formula works with negative numbers too.
//
Use / for means
// truncates toward −∞. Use / so averages keep decimals.
Analysis
Time and Space Complexity
Program
Time
Extra space
Running sum / list / built-in
O(N)
O(1) beyond the input
Storing all inputs first
O(N)
O(N)
One pass is enough. You do not need to sort for an arithmetic mean.
Remember
Key Takeaways
Formula: average = sum / N.
Guard: never divide when N is zero.
Python: use / (not //) so means keep decimals.
Test: 1…5 → 3; 10…30 step 5 → 20; 1,2 → 1.5.
One line: add every value, divide by the count — that is the average.
Frequently Asked Questions
Add all N numbers, then divide by N. Formula: average = sum / N. In Python 3, / keeps a float result.
Division by zero is undefined. If the count is 0 (empty list), do not compute an average.
No. Average is sum divided by count. Median is the middle value after sorting.
Yes. For a non-empty list: sum(values) / len(values). Always guard empty lists before dividing.
O(N) — each value is visited once. A running sum uses O(1) extra space.