Store the data
A common way to hold table-like data is a list of dictionaries: one dictionary per row, with the column names as keys.
students = [
{"name": "Arta", "class": "8A", "maths": 92, "science": 81},
{"name": "Dren", "class": "8B", "maths": 67, "science": 74},
{"name": "Bora", "class": "8A", "maths": 78, "science": 88},
{"name": "Leon", "class": "8B", "maths": 85, "science": 59},
{"name": "Ina", "class": "8A", "maths": 54, "science": 70},
]
print(len(students), "students")
print(students[0]["name"], "scored", students[0]["maths"], "in maths")
Real data usually comes from a file. The Python files and CSV tutorial shows how to load rows like these from a spreadsheet.
Averages and medians
from statistics import mean, median
students = [
{"name": "Arta", "class": "8A", "maths": 92, "science": 81},
{"name": "Dren", "class": "8B", "maths": 67, "science": 74},
{"name": "Bora", "class": "8A", "maths": 78, "science": 88},
{"name": "Leon", "class": "8B", "maths": 85, "science": 59},
{"name": "Ina", "class": "8A", "maths": 54, "science": 70},
]
maths = [s["maths"] for s in students]
print("Average:", round(mean(maths), 1))
print("Median:", median(maths))
print("Highest:", max(maths), "Lowest:", min(maths))
The line [s["maths"] for s in students] is a list comprehension: it builds a new list by taking one value from each row. The median is the middle value when the numbers are sorted. It is less affected than the average by one very high or very low score.
Filtering and sorting
students = [
{"name": "Arta", "class": "8A", "maths": 92, "science": 81},
{"name": "Dren", "class": "8B", "maths": 67, "science": 74},
{"name": "Bora", "class": "8A", "maths": 78, "science": 88},
{"name": "Leon", "class": "8B", "maths": 85, "science": 59},
{"name": "Ina", "class": "8A", "maths": 54, "science": 70},
]
class_8a = [s for s in students if s["class"] == "8A"]
print("8A:", [s["name"] for s in class_8a])
ranked = sorted(students, key=lambda s: s["maths"], reverse=True)
for place, s in enumerate(ranked, start=1):
print(place, s["name"], s["maths"])
key=lambda s: s["maths"] tells sorted which value to compare. enumerate(..., start=1) numbers the rows from 1 instead of 0.
Counting and grouping
from collections import Counter, defaultdict
students = [
{"name": "Arta", "class": "8A", "maths": 92, "science": 81},
{"name": "Dren", "class": "8B", "maths": 67, "science": 74},
{"name": "Bora", "class": "8A", "maths": 78, "science": 88},
{"name": "Leon", "class": "8B", "maths": 85, "science": 59},
{"name": "Ina", "class": "8A", "maths": 54, "science": 70},
]
print(Counter(s["class"] for s in students))
by_class = defaultdict(list)
for s in students:
by_class[s["class"]].append(s["science"])
for name, scores in sorted(by_class.items()):
print(name, "science average:", round(sum(scores) / len(scores), 1))
Counter counts how often each value appears. defaultdict(list) creates an empty list the first time a new class name is used, so you can append without checking first.
A quick text bar chart
scores = {"Arta": 92, "Dren": 67, "Bora": 78, "Leon": 85, "Ina": 54}
for name, score in scores.items():
bar = "#" * (score // 5)
print(f"{name:<5} {bar} {score}")
Each # stands for 5 points. {name:<5} pads the name to 5 characters so the bars line up. When you want real charts, the next step is the pandas and matplotlib libraries, which use these same ideas on much larger data.
Try it yourself
In Unays Code, add a "total" for each student (maths plus science), print the top three by total, and find which class has the higher maths median. Always ask whether a result makes sense: with only five students, one score can change an average a lot.
This tutorial was written with AI assistance and reviewed by Unays. The runnable code examples were tested before publishing. Spotted a mistake? Tell us at [email protected].