Definition
Who scored highest, and what was the average? Can your program tell you in a second?
This project reads a CSV file of marks, computes summary statistics such as the average, highest, lowest and pass rate for each subject and each learner, and prints a clear report, combining files, dictionaries, loops, functions and formatting.
In simple words
First the program writes a small marks file, so it runs anywhere. Then it reads the file, turns the text into numbers, and works out the numbers a teacher or coach would want to see. This is exactly what the first step of a data science project looks like.
Why it matters
Reading data, cleaning it and summarising it is the core of data analysis and the first stage of every machine learning project. In the next module you'll do the same with pandas, in a few lines.
How it works
- Read the CSV into a list of rows.
- Convert marks from text to numbers.
- For each subject: average, highest and lowest.
- For each learner: total and average; find the topper.
- Count how many passed every subject (40 or more).
- Print a neatly formatted report.
In code
The whole analyser: write a sample file, read it, compute statistics and print a report.
import csv
from statistics import mean
rows = [["name", "maths", "science", "english"],
["Aarav", 72, 65, 80], ["Meera", 91, 88, 85], ["Kabir", 38, 55, 60],
["Sana", 85, 79, 92], ["Ishaan", 64, 35, 70]]
with open("marks.csv", "w", newline="") as f:
csv.writer(f).writerows(rows)
with open("marks.csv") as f:
records = list(csv.DictReader(f))
subjects = ["maths", "science", "english"]
for record in records:
for subject in subjects:
record[subject] = int(record[subject])
print("Subject report")
for subject in subjects:
scores = [r[subject] for r in records]
print(f" {subject:<8} average {mean(scores):5.1f} high {max(scores)} low {min(scores)}")
def average(record):
return mean(record[s] for s in subjects)
topper = max(records, key=average)
print(f"Topper: {topper['name']} with an average of {average(topper):.1f}")
passed_all = [r["name"] for r in records if all(r[s] >= 40 for s in subjects)]
print(f"Passed every subject: {len(passed_all)} of {len(records)} -> {passed_all}")Output
Subject report maths average 70.0 high 91 low 38 science average 64.4 high 88 low 35 english average 77.4 high 92 low 60 Topper: Meera with an average of 88.0 Passed every subject: 3 of 5 -> ['Aarav', 'Meera', 'Sana']
Try it yourself
Project checklist
Work through these in the runner above, then extend the analyser.
- Run it and check one subject's average by hand.
- Add a sixth learner to the rows and run again.
- Print each learner's grade using the grade() function from the conditions topic.
- Find the subject with the lowest average and print it.
Think about it
- Which step took the most lines of code? In pandas, the same work takes about five lines.
Common mistakes
- Forgetting to convert marks from text, so max('9', '10') gives '9'.
- Hard-coding the number of learners instead of using len().
- Printing a report so messy that nobody can read it. Format with f-strings.
Check yourself
Summary
- Read the CSV, convert text to numbers, then summarise.
- Averages, highs, lows and pass rates tell the story of the data.
- This is the first stage of every data and ML project.
Finished reading?