NEBians
search Sign In Register

Python Data Detective

Lesson 3 of 4 Coding game schedule15 min

Loading simulation…

sports_esportsPlay with the blocks and run your code

flagWhat you'll discover

  • arrow_forwardTotal a list with the accumulator pattern
  • arrow_forwardCount items matching a condition inside a loop
  • arrow_forwardUse len(), max() and min() on real data
  • arrow_forwardCompute a percentage and format a report with f-strings

From numbers to answers

A list like [78, 45, 92, ...] is data; "the average is 64 and 80% passed" is information. Turning one into the other is data analysis, and its core moves are tiny: count things, add things up, find extremes, compute ratios. Every dashboard, every report card and every "students improved 12% this year" headline comes from exactly these operations run on bigger lists. Master them on ten scores and you have the same toolkit analysts use on ten million rows.

One loop, several jobs

You could loop once to total the scores and loop again to count passes — but one loop can do both. Keep two accumulators: total += s on every item, and passed += 1 only inside if s >= PASS_MARK. Each variable accumulates a different fact during the same pass over the data. This is how real data pipelines stay fast: one sweep through the data, collecting everything needed at once, instead of re-reading it for every question.

Rates: counts becoming percentages

A pass count of 8 means little until you know there were 10 students; 8 out of 10 is the insight. That is a rate: passed / count gives 0.8, and multiplying by 100 turns it into 80 — a percentage. Rates let you compare fairly across groups of different sizes: a class of 10 and a school of 1000 can both score "80% passed". Watch the order of operations: passed / count * 100 works left to right, dividing first, exactly as intended.

Built-ins are tested tools

Python ships with len(), max(), min() and sum() because counting and extremes are needed in nearly every program ever written. Could you find the maximum with your own loop and a best-so-far variable? Absolutely — you did it in JavaScript Quest. Should you, when max() exists? Usually not: built-ins are shorter, faster and already debugged by thousands of users. A professional rule of thumb: write the manual version once to understand it, then reach for the built-in forever after.

quizCheck your knowledge

1. What is the accumulator pattern for totalling a list?
2. 8 of 10 students passed. Which expression gives the pass rate as a percentage?
3. Why count passes inside the same loop that totals the scores?