DEV Community

Shahrouz Nikseresht
Shahrouz Nikseresht

Posted on

Day 55: Python Calculate Mean & Median Manually – No Statistics Module, Just Pure Python Power

Welcome to Day 55 of the #80DaysOfChallenges journey! This intermediate challenge forces you to implement mean and median from scratch, without using the statistics module or any external library. You’ll do it the old-school way: sum + division for mean, sort + middle element(s) for median. It’s short, beautiful, and teaches you exactly how these fundamental stats work under the hood.

Perfect for interviews (they love asking this), data science beginners who want to truly understand their tools, or anyone who enjoys that "I built this myself" satisfaction.

Example:

[1, 3, 5, 7, 9] → mean = 5.0, median = 5

[1, 3, 5, 7] → mean = 4.0, median = 4.0 (average of 3 and 5)


💡 Key Takeaways from Day 55: Manual Stats Calculator

This challenge delivers a rock-solid function that handles empty lists, odd/even lengths, and returns clean results in a dict. We'll break it down: safe empty handling, mean with built-in sum, median with sort and smart indexing, and interactive main.

1. Function Design: Empty List Guard First

def calculate_stats(nums: list[int]) -> dict:
    if not nums:
        return {'mean': None, 'median': None}  # handle empty input safely

    n = len(nums)
Enter fullscreen mode Exit fullscreen mode

Immediate empty check with None returns, prevents division by zero and makes the function safe for real data pipelines.

2. Mean: One Line, Maximum Clarity

    # Mean
    mean_value = sum(nums) / n
Enter fullscreen mode Exit fullscreen mode

That's it. Python's built-in sum() is highly optimized (written in C), so this is both readable and fast. No need to reinvent the wheel here.

3. Median: Sort Once, Smart Middle Selection

    # Median
    sorted_nums = sorted(nums)
    mid = n // 2

    if n % 2 == 1:
        median_value = sorted_nums[mid]   # odd length -> middle number
    else:
        median_value = (sorted_nums[mid] + sorted_nums[mid - 1]) / 2   # even length
Enter fullscreen mode Exit fullscreen mode

Sort once (O(n log n), unavoidable for median), then:

  • Odd length: take exact middle
  • Even length: average the two middle numbers

This is the classic textbook method. Clean, correct, and handles everything.

4. Return Dict: Clean Output Structure

    return {
        'mean': mean_value,
        'median': median_value
    }
Enter fullscreen mode Exit fullscreen mode

Beautiful dict return, perfect for JSON, APIs, or further processing.

5. Main Interactive: Real Input Testing

raw = input("Enter numbers (space-separated): ").strip()

if raw == "":
    print("No input provided. Exiting.")
else:
    nums = list(map(int, raw.split()))
    stats = calculate_stats(nums)
    print("Mean:", stats['mean']")
    print("Median:", stats['median'])
Enter fullscreen mode Exit fullscreen mode

Robust input handling, immediate feedback. Try duplicates, negatives, single numbers, works perfectly.


🎯 Summary and Reflections

This manual stats calculator is tiny but powerful, teaching you the exact algorithms behind statistics.mean() and statistics.median(). It reinforced:

  • Never trust black boxes: Know how your tools work.
  • Sort once rule: For multiple stats, sort only once.
  • Edge cases matter: Empty list, one element, even/odd length.

This code is production-ready. I use this pattern in data validation scripts all the time.

Advanced Alternatives:

  • Use heapq for streaming median (O(log n) per insert)
  • NumPy version for speed on huge arrays
  • Return mode too with Counter

But for clarity and teaching, this version is unbeatable.


🚀 Next Steps and Resources

Day 55 showed you the soul of statistical functions. Now you truly understand mean and median!

Drop your favorite manual stats implementation below, I read them all! 🔥

Top comments (0)