Mini-Project: Grade Statistics
Before you start
#What you'll build
A small report generator that takes a list of grades for a class and prints: mean, min, max, total students, number passing (>= 60), pass rate (%), and the list of students above 90.
Sample interaction
Class Grade Report
==================
Total students: 30
Average grade: 71.5
Highest grade: 98
Lowest grade: 31
Passing (>= 60): 23 (76.7%)
Top scorers (>= 90): [92, 95, 90, 91, 98]
#What you'll learn
After this lesson, you will be able to:
- Combine sum(), min(), max() and len() to summarise a list of numbers
- Filter a list with a comprehension and an if clause
- Format percentages and rounded numbers with f-string format specs
#Step 1: A hardcoded list of grades
Start with a list of 10 grades. Hardcoded is fine — it lets you focus on the stats logic without input handling getting in the way.
grades = [85, 92, 78, 45, 67, 71, 88, 33, 95, 58]
print(grades)This is your dataset. Every step below is a question we'll ask of this same list.
What does `len(grades)` return?
#Step 2: Mean, min, max
sum, min, max, and len are all built-in — no imports needed. The mean is sum / len.total = sum(grades)
average = total / len(grades)
highest = max(grades)
lowest = min(grades)
print(f"Average: {average:.1f}")
print(f"Highest: {highest}")
print(f"Lowest: {lowest}"):.1f rounds to one decimal place — cleaner than printing 71.2999999. Tweak the precision based on the audience (.2f for currency, .0f for whole-number percentages).What does sum([85, 92, 78]) / 3 produce?
#Step 3: Filter to passing grades with a comprehension
if to filter.passing = [g for g in grades if g >= 60]
print(passing) # [85, 92, 78, 67, 71, 88, 95]
print(len(passing)) # 7 -- the countpassing.append(g).What does this print? grades = [85, 45, 92] passing = [g for g in grades if g >= 60] print(grades)
What does `[g for g in grades if g >= 60]` build?
#Step 4: Pass rate and top scorers
len(grades) to get a percentage. Then do another comprehension for top scorers.pass_rate = len(passing) / len(grades) * 100
top = [g for g in grades if g >= 90]
print(f"Passing: {len(passing)} / {len(grades)} ({pass_rate:.1f}%)")
print(f"Top scorers (>= 90): {top}")pass_rate is a float — :.1f formats it cleanly. This is the same skill you'll use later to print model accuracy: f"Accuracy: {accuracy:.1%}".What does this print? rate = 0.847 print(f"{rate:.1%}")
% format spec is doing TWO things at once: multiplying by 100 AND appending the percent sign. So 0.847 becomes 84.7, then .1 rounds to one decimal place, then % gets added. A common bug: writing f"{rate*100:.1%}" produces 8470.0% because you've multiplied twice. Either use :.1% on the raw fraction, or use :.1f and add % manually — never mix them.#Step 5: Put it all together as a report
grades = [85, 92, 78, 45, 67, 71, 88, 33, 95, 58, 73, 81]
print("Class Grade Report")
print("=" * 18)
print(f"Total students: {len(grades)}")
print(f"Average grade: {sum(grades) / len(grades):.1f}")
print(f"Highest grade: {max(grades)}")
print(f"Lowest grade: {min(grades)}")
passing = [g for g in grades if g >= 60]
top = [g for g in grades if g >= 90]
pass_rate = len(passing) / len(grades) * 100
print(f"Passing (>= 60): {len(passing)} ({pass_rate:.1f}%)")
print(f"Top scorers (>= 90): {top}")Twelve students, one report. Run it — you have a real piece of data analysis written entirely in standard library Python.
#Now build it yourself
Class of 30 — full stats report
You're given a list of 30 grades. Write a function `report(grades)` that prints the full class report: count, average, min, max, count passing (>= 60), pass rate %, and the list of top scorers (>= 90). Use list comprehensions for the filters and built-in aggregates for everything else.
Class Grade Report
==================
Total students: 30
Average grade: 71.5
Highest grade: 98
Lowest grade: 31
Passing (>= 60): 23 (76.7%)
Top scorers (>= 90): [92, 95, 90, 91, 98]#Now make it your own
Try one of these variations:
- Letter grades. Write a helper
letter_grade(g)that returns 'A' (>=90), 'B' (>=80), 'C' (>=70), 'D' (>=60), 'F' (<60). Print a count for each letter using a dict comprehension. - Standard deviation. Compute the standard deviation of the class.
((sum([(g - mean) ** 2 for g in grades])) / len(grades)) ** 0.5. Print it alongside the average. - Bar chart with stars — bucket grades into 10s (0-9, 10-19, ...) and print a horizontal bar chart using
*. A row like90-99: *****(5 stars = 5 students).