TalentJR

🐍 Python for kids · Class 5 · Lesson 12 of 18 · Free

Data Processing – Counting, Totals and Averages

⏱ About 30 minutes · ▶ run real Python here — nothing to install · no sign-in

In this lesson you will

  • Count and total values in a list with a loop.
  • Work out an average and filter items into a new list.
  • Print data as a neat table with f-strings.

Your class teacher has 40 test papers to check. She must find the class average, the topper and who needs help. With Python, that whole job takes a few lines.

What is data processing?

Data is a collection of facts, like marks, runs or temperatures. Processing means turning those facts into useful answers.

Every data job has three steps. First collect the data, then work on it, then show the result.

You already know the tools. Lists hold the data, loops visit each item, and if picks items.

🐍 Python — you can edit this code
Output
Days recorded: 5
Hottest day: 36
Coolest day: 29

💡 These are a week's temperatures in Chennai. Five facts became three useful answers.

How do I count items that match a condition?

To count, use a counter variable that starts at 0. Each time an item matches, add 1 to it.

This is the piggy-bank idea from Lesson 8. But now we add 1 only when the if is True.

Below, we count how many students passed with 35 or more.

🎬 How Python thinks — step 1 of 18Python is about to run line 1
▶marks = [72, 28, 90, 45, 31]
2passed = 0
3for m in marks:
4 if m >= 35:
5 passed = passed + 1
6print(f"{passed} out of {len(marks)} students passed.")
📦 Variables (memory boxes)

No variables yet.

🖥️ Output
▌
✏️ Change this code and run it yourself
🐍 Python — you can edit this code
Output
3 out of 5 students passed.

How do I find the total and the average?

The average is the total divided by how many items there are. Python gives sum() and len() for this.

Dividing with / can give a long decimal, like 66.6666. round(number, 1) keeps just one digit after the point.

Here a batter's runs in five matches give us the batting average.

🎬 How Python thinks — step 1 of 6Python is about to run line 1
▶runs = [45, 12, 78, 30, 9]
2total = sum(runs)
3average = total / len(runs)
4print("Total runs:", total)
5print("Average:", round(average, 1))
📦 Variables (memory boxes)

No variables yet.

🖥️ Output
▌
✏️ Change this code and run it yourself
🐍 Python — you can edit this code
Output
Total runs: 174
Average: 34.8

💡 If the list is empty, len() is 0 and dividing by 0 is an error. Check len() first.

How do I filter data into a new list?

Filtering means keeping only the items you want. We make an empty list, then append() each matching item.

The old list stays the same. The new list holds just the chosen items.

Here we pick out the scores of 50 or more, the half-centuries.

🎬 How Python thinks — step 1 of 21Python is about to run line 1
▶scores = [45, 67, 12, 88, 50, 23]
2big = []
3for s in scores:
4 if s >= 50:
5 big.append(s)
6print("Half-centuries:", big)
7print("How many:", len(big))
📦 Variables (memory boxes)

No variables yet.

🖥️ Output
▌
✏️ Change this code and run it yourself
🐍 Python — you can edit this code
Output
Half-centuries: [67, 88, 50]
How many: 3

How do I collect data from the user and process it?

Real data often comes from the user. A loop with input() and append() fills the list.

After the loop, we process the full list. The steps are always collect, work, show.

This program asks for the pocket money spent on three days.

🐍 Python — you can edit this code
Output
Rupees spent on day 1: 20
Rupees spent on day 2: 35
Rupees spent on day 3: 10
Total spent: 65 rupees
Biggest spend: 35 rupees

How do I show data as a neat table?

A table is easier to read than a long sentence. Each row shows one item, in neat columns.

In an f-string, {name:<8} pads the text to 8 spaces wide. Then every column lines up.

We loop over a dictionary with items() to print one row per student.

🐍 Python — you can edit this code
Output
Name    Marks
-------------
Aarav   85
Diya    92
Kabir   67
Class average: 81.3

💡 "-" * 13 repeats the dash 13 times to draw a line. It is quicker than typing every dash.

🤔 Predict the output

Think like Python: read the code and choose what it prints. You get the reason straight away.

Question 1. What does this print?

nums = [4, 9, 2, 7]
count = 0
for n in nums:
    if n > 5:
        count = count + 1
print(count)

Question 2. What does this print?

marks = [80, 90, 70]
print(sum(marks) / len(marks))

Question 3. What does this print?

words = ["cat", "elephant", "dog", "tiger"]
long = []
for w in words:
    if len(w) > 3:
        long.append(w)
print(long)

🧩 Your challenge

Make a class result report. Ask for the marks of 5 students, then print the class average, the highest mark, and a new list of marks below 40 (students who need extra help).

Expected output (with the inputs given):
Marks of student 1: 78
Marks of student 2: 35
Marks of student 3: 92
Marks of student 4: 60
Marks of student 5: 25
Class average: 58.0
Highest mark: 92
Needs extra help: [35, 25]
🐍 Python — you can edit this code

⚠️ Common mistakes (and how to fix them)

❌ Wrong

runs = [10, 20, 30]
for r in runs:
    total = 0
    total = total + r
print(total)

No error message – but it prints 30, not the total 60.

✅ Right

runs = [10, 20, 30]
total = 0
for r in runs:
    total = total + r
print(total)

total = 0 inside the loop resets the total every round. Start the total once, before the loop.

❌ Wrong

marks = [80, 90, 70]
average = sum(marks) / 3
marks.append(100)
print(average)

No error message – but the average still leaves out the new mark 100.

✅ Right

marks = [80, 90, 70]
marks.append(100)
average = sum(marks) / len(marks)
print(average)

Python works out average once, when that line runs. Collect all data first, and use len() instead of a fixed number.

❌ Wrong

marks = []
for i in range(2):
    m = input("Marks: ")
    marks.append(m)
print(sum(marks))

TypeError: unsupported operand type(s) for +: 'int' and 'str'

✅ Right

marks = []
for i in range(2):
    m = int(input("Marks: "))
    marks.append(m)
print(sum(marks))

input() always gives text. Wrap it in int() so the list holds numbers that sum() can add.

📌 Remember

  • Data jobs have three steps: collect the data, work on it, show the result.
  • Count with a counter that starts at 0 and adds 1 when an if matches.
  • Average = sum(list) / len(list); round(x, 1) keeps one decimal digit.
  • Filter by appending matching items to a new, empty list.
  • Start totals and counters before the loop, not inside it.
  • f-strings like {name:<8} line up columns in a neat table.

Finished the quiz and the challenge?

Questions students ask

How do I find the average of a list in Python?

Divide the total by the number of items: sum(my_list) / len(my_list). Make sure the list is not empty first.

What does round() do in Python?

round(x, 1) rounds a number to one digit after the decimal point. round(66.67, 1) gives 66.7.

Does filtering change the original list?

No. Filtering with append() builds a new list. The original list keeps all its items.

Why does sum() give an error with input() values?

input() gives text, not numbers, and sum() cannot add text. Use int(input()) so each item is a number.