Python Average of N Numbers Program

Beginner
5 min read
Updated: Oct 2026
3 programs
Live preview

What Is the Average of N Numbers?

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
ValuesSumNAverage
1, 2, 3, 4, 51553
10, 15, 20, 25, 30100520
2, 4623
1, 2321.5

How to Find the Average of N Numbers in Python

Sum every value once, then divide by the count — guard empty input first.

  1. Reject N <= 0 or an empty list (no division by zero).
  2. Iterate the N values (loop, sum, or statistics.mean).
  3. Accumulate a running sum.
  4. Return sum / N (Python 3 / keeps a float).
MethodIdeaTimeBest for
Running sum + NLoop N times with a fixed listO(N)Interviews, demos
List helperSum len(values) itemsO(N)Reusable default
sum / statistics.meanBuilt-in one-linersO(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

Cheat Sheet

Keep these facts handy while writing the program.

TopicDetail
Formulaaverage = sum / N
GuardNever divide when N <= 0
Python 3/ keeps decimals (// truncates)
Not medianMedian needs sorting; average does not
1..5sum 15 → average 3

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

Worked Walkthrough — 1, 2, 3, 4, 5

Add every value, then divide by the count.

Calculation
values: 1, 2, 3, 4, 5
N = 5

sum = 0
  + 1 → 1
  + 2 → 3
  + 3 → 6
  + 4 → 10
  + 5 → 15

average = 15 / 5 = 3

Python Programs

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}")

How It Works

  1. Return 0 if n <= 0 (documented sentinel).
  2. Add values[0] through values[n - 1] into total.
  3. Divide once after the loop: total / n.
  4. Python 3 / keeps decimals, so 1 / 2 is 0.5.
  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}")

How It Works

  1. Reject an empty list before dividing.
  2. Sum every element in one forward pass.
  3. Divide by len(values) for the mean.
  4. 10+15+20+25+30 = 100 → average 20.0.
  5. 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))

How It Works

  1. Return early when the list is empty.
  2. sum(values) adds every element; divide by len(values).
  3. statistics.mean does the same job and raises on an empty sequence.
  4. Same math as the for loop — just more compact.
  5. 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.

Time and Space Complexity

ProgramTimeExtra space
Running sum / list / built-inO(N)O(1) beyond the input
Storing all inputs firstO(N)O(N)

One pass is enough. You do not need to sort for an arithmetic mean.

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.

Next: Biggest of Three Numbers

Find the largest among three values with simple comparisons.

Biggest of three tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful