What it shows
The program reads marks from a CSV file and finds the mean, median, highest, lowest and pass percentage. It shows how code can analyse data that a spreadsheet also holds.
The science
CSV is plain text with values separated by commas, readable by Python and spreadsheets. The csv module splits each line into fields. The statistics module gives the mean (average), median (middle value) and mode. A gap between mean and median tells you if a few very high or low marks pull the average.
Materials
Cost: free. Many items come from home or from waste; spare parts such as LEDs, motors and wire are in the store.
- A computer, laptop or phone with a browser (free)
- Python 3 with IDLE, or the free Thonny editor, or an online Python runner (all free)
- A CSV file marks.csv made in Notepad or a spreadsheet with a header name,maths and about 20 made-up students
- A notebook and a pencil
Safety
This project uses safe, everyday materials. Still, read the rules below with a parent before you start.
Never use mains electricity: power models with cells, batteries or a low-voltage adapter. Sharp and hot tools, flames and lab chemicals need an adult or your teacher beside you. Keep magnets, button cells and small parts away from young children, and wash your hands after making.
All the safety rulesStep by step
8 steps. Do them in order, and test each part before you go on to the next. Write what you notice in a notebook as you go.
In Notepad write the first line name,maths and then 20 lines such as Asha,72 and save as marks.csv in the same folder as your program (use made-up names).
Create analyse.py with import csv and import statistics on the first lines.
Open the file: marks = [] then with open("marks.csv") as f: and reader = csv.DictReader(f).
Inside the with, add for row in reader: marks.append(int(row["maths"])).
Print the count with print("Students:", len(marks)) and the mean with print("Mean:", round(statistics.mean(marks), 1)).
Print the median with statistics.median(marks), the highest with max(marks) and the lowest with min(marks).
Count passes with passed = len([m for m in marks if m >= 33]) and print the percentage with round(passed * 100 / len(marks), 1).
Compare the numbers with the same calculations in a spreadsheet using AVERAGE, MEDIAN, MAX and MIN.
What you should see
The program prints the number of students, mean, median, highest, lowest and pass percentage. These agree with the spreadsheet values, which proves both are correct.
Viva questions
A teacher or a judge will ask how it works. Try to answer each question before you open it, then compare.
1What is a CSV file?
A text file where each line is a row and commas separate the columns.
2What is the difference between mean and median?
The mean is the average; the median is the middle value when sorted.
3Why convert with int()?
The csv module reads everything as text.
4What does DictReader do?
It reads each row as a dictionary using the header names as keys.
Take it further
Add a second subject column and print the student with the best total, and draw a bar of stars for each mark range.
Change one thing, measure what happens, and add a table or chart of your readings: that is what turns a model into your own investigation.
Free ebooks for this project
Computer & office
Excel Formulas Workbook
Sixty formulas and functions, each with a worked example from school, shop or office
