Lesson: Small Project — Analyze Class Test Scores
Introduction
Today, you'll be a data detective! We'll use Python to look at a small set of class test scores and answer useful questions, like:
- What is the class average?
- Who scored the highest and lowest?
- How many students passed?
- Who are the top performers?
This mini-project uses simple Python skills: lists, loops, if-statements, and a little printing.
What You'll Build
A tiny "score analyzer" that:
- Stores names and scores
- Calculates average, highest, and lowest
- Counts passes and fails
- Shows a simple text chart of scores
Step-by-Step Plan
- Set up the data — We'll use a list of dictionaries: each student has a name and a score.
- Calculate basic stats — Class average, highest and lowest score, and who got them
- Count pass/fail — Decide a pass mark (for example, 50)
- Find top performers — Sort by score and list the top 3
- Make a simple text chart — Print bars made of stars to "see" the scores
- (Optional) Try your own data — Add more students or change scores
Python Code Examples
Code Example 1 — Set Up Data and Basic Stats
This code builds the data and finds the average, highest, and lowest.
# Small Project: Analyze Class Test Scores
# Code Example 1: Set up data and basic stats
# Our class test scores (you can edit these!)
students = [
{"name": "Ava", "score": 78},
{"name": "Ben", "score": 92},
{"name": "Cara", "score": 66},
{"name": "Dev", "score": 45},
{"name": "Eli", "score": 88},
{"name": "Fia", "score": 54},
{"name": "Gio", "score": 99},
]
# 1) Calculate class average
total = 0
for s in students:
total += s["score"]
count = len(students)
average = total / count # careful: make sure count > 0
print(f"Class average: {average:.1f}")
# 2) Find highest and lowest
# Start by assuming the first student is both highest and lowest
highest = students[0]
lowest = students[0]
for s in students:
if s["score"] > highest["score"]:
highest = s
if s["score"] < lowest["score"]:
lowest = s
print(f"Highest: {highest['name']} with {highest['score']}")
print(f"Lowest: {lowest['name']} with {lowest['score']}")What's Happening Here
- We loop through the list to add up scores for the average.
- We track the highest and lowest by comparing each score.
Code Example 2 — Pass/Fail and Top Performers
Let's count how many passed and list the top 3 students.
# Code Example 2: Pass/Fail count and top performers
pass_mark = 50
passes = 0
fails = 0
for s in students:
if s["score"] >= pass_mark:
passes += 1
else:
fails += 1
print(f"Passed: {passes}")
print(f"Failed: {fails}")
# Sort students by score (highest first)
# The 'key' tells Python to sort using the score
sorted_students = sorted(students, key=lambda x: x["score"], reverse=True)
# Top 3 performers (or fewer if the class is small)
top_n = 3
top_students = sorted_students[:top_n]
print("Top performers:")
for rank, s in enumerate(top_students, start=1):
print(f" {rank}. {s['name']} - {s['score']}")What's Happening Here
- We use if-statements to count passes and fails.
- We sort the list with
sorted(..., key=..., reverse=True). - We slice the list
[:3]to get the top 3.
Code Example 3 — Simple Text Chart (Bar Chart with Stars)
Let's "draw" the scores using stars. One star for every 5 points.
# Code Example 3: Simple text-based bar chart
print("\nScore Chart (one * per 5 points):")
for s in students:
# // is integer division: 78 // 5 = 15
stars = s["score"] // 5
bar = "*" * stars
print(f"{s['name']:>4}: {bar} ({s['score']})")What's Happening Here
- We scale scores down so we don't print too many stars.
f"{s['name']:>4}"right-aligns names for a neat look.
Small Practical Exercise 🎯
Your mission: improve the analyzer!
Tasks:
- Add a new student with a score to the list.
- Change the
pass_markto 60 and run the code again. How do the pass/fail numbers change? - Curve the scores: add 5 bonus points to every score, but don't let any score go above 100.
- After curving, print the new class average and the new top 3 students.
Starter Code (fill in the TODOs):
students = [
{"name": "Ava", "score": 78},
{"name": "Ben", "score": 92},
{"name": "Cara", "score": 66},
{"name": "Dev", "score": 45},
{"name": "Eli", "score": 88},
{"name": "Fia", "score": 54},
{"name": "Gio", "score": 99},
]
# 1) TODO: Add a new student, for example: {"name": "Hana", "score": 73}
pass_mark = 60 # 2) Changed pass mark
# 3) Curve scores by +5, but max 100
for s in students:
new_score = s["score"] + 5
if new_score > 100:
new_score = 100
s["score"] = new_score
# Recalculate average
total = 0
for s in students:
total += s["score"]
average = total / len(students)
print(f"New class average after curving: {average:.1f}")
# Count passes/fails with the new pass_mark
passes = 0
fails = 0
for s in students:
if s["score"] >= pass_mark:
passes += 1
else:
fails += 1
print(f"Passed: {passes}, Failed: {fails}")
# Top 3 students after curving
sorted_students = sorted(students, key=lambda x: x["score"], reverse=True)
print("Top 3 after curving:")
for i, s in enumerate(sorted_students[:3], start=1):
print(f" {i}. {s['name']} - {s['score']}")Tips
- Change the data and run again to see how results change.
- Try different pass marks (like 40, 50, 70) and compare.
- Add more students to make your chart longer.
Recap
- You built a small Python project that:
- Stored and analyzed class test scores
- Calculated average, highest, and lowest
- Counted passes and fails
- Listed top performers
- Drew a simple text chart
- These are the same steps used in real-world data analysis: collect data, compute basic stats, sort/filter, and visualize. You're on your way to becoming a data pro!