carrie_edwards
carrie_edwards 2d ago β€’ 0 views

Average vs. Median in Python: Grade 7 Web Basics Explanation

Hey everyone! πŸ‘‹ Ms. Davis here. Today we're tackling average vs. median in Python - super important for web basics! Let's break it down so it's easy to understand. Get ready to code! πŸ’»
πŸ’» Computer Science & Technology

1 Answers

βœ… Best Answer
User Avatar
davis.alison86 Dec 28, 2025

πŸ“š Understanding Average vs. Median in Python

In Python, both average and median help describe the 'center' of a set of numbers, but they do it in different ways. Think of it like this: imagine you and your friends are comparing how many hours of video games you played last week. Average and median give you different perspectives!

  • πŸ”’ Average (Mean): The average is what you get when you add up all the numbers and then divide by how many numbers there are. It's like sharing equally. In Python, you'd use sum() and len(). Mathematically, it's expressed as: $\text{Average} = \frac{\text{Sum of Numbers}}{\text{Number of Numbers}}$
  • πŸ“Š Median: The median is the middle number when your numbers are listed from smallest to largest. If you have an even number of numbers, you take the average of the two middle ones. It's helpful because it's not as affected by very high or very low numbers.

πŸ’» Python Code Examples

Let's see how to calculate the average and median in Python!


numbers = [10, 12, 15, 20, 22]

# Calculate the average
average = sum(numbers) / len(numbers)
print("Average:", average)

# Calculate the median
numbers.sort() # Sort the list first!
if len(numbers) % 2 == 0:
    # Even number of elements
    mid1 = numbers[len(numbers) // 2 - 1]
    mid2 = numbers[len(numbers) // 2]
    median = (mid1 + mid2) / 2
else:
    # Odd number of elements
    median = numbers[len(numbers) // 2]
print("Median:", median)

πŸ€” When to Use Which?

  • βš–οΈ Use Average: When you want to know the 'typical' value, and extreme values won't skew the result too much. For example, average test scores.
  • πŸ›‘οΈ Use Median: When you have extreme values (outliers) that could distort the average. For example, home prices – a few very expensive houses can make the average price seem higher than what most people actually pay.

πŸ’‘ Key Differences Summarized

Feature Average (Mean) Median
Calculation Sum of all values divided by the number of values Middle value when values are sorted
Sensitivity to Outliers Highly sensitive Less sensitive
Best Use Case Data without extreme values Data with extreme values

✍️ Practice Quiz

Let's test your understanding. Calculate the average and median for the following sets of numbers:

  1. πŸ“Š Data set: [5, 7, 9, 11, 13]
  2. πŸ“ˆ Data set: [2, 4, 6, 8, 10, 12]
  3. πŸ“‰ Data set: [1, 2, 3, 4, 100]

Answers: 1. Average: 9, Median: 9; 2. Average: 7, Median: 7; 3. Average: 22, Median: 3

πŸŽ‰ Challenge Problems!

Time for a little challenge! Create Python functions to calculate the average and median of any given list of numbers. Remember to handle both even and odd length lists correctly when finding the median!

Join the discussion

Please log in to post your answer.

Log In

Earn 2 Points for answering. If your answer is selected as the best, you'll get +20 Points! πŸš€