Scenario: Data for a small class is stored in a 2D array. Each row is a student and each column is a subject: Math, Science and English.
Data
student_scores = [
# Math, Science, English
[85, 90, 88], # Student 1
[76, 88, 92], # Student 2
[94, 78, 81], # Student 3
[89, 95, 91] # Student 4
]
Your tasks
- Counting — count how many scores in the entire class are 90 or above.
- Totalling — calculate and print the total score for Student 3 (row index 2).
- Averaging — calculate the average score for the Science test (column index 1).
- Maximum and minimum — find the highest score in English (column 2) and the lowest score in Math (column 0).
💡 Try every task in your editor first. Only reveal the solution when you have had a proper go.
Solution
student_scores = [
[85, 90, 88],
[76, 88, 92],
[94, 78, 81],
[89, 95, 91]
]
# Question 1: Counting
count = 0
for row in student_scores:
for score in row:
if score >= 90:
count = count + 1
print("Scores of 90 or above:", count)
# Question 2: Totalling Student 3
total = 0
for score in student_scores[2]:
total = total + score
print("Student 3 total:", total)
# Question 3: Science average (column 1)
science_total = 0
for r in range(len(student_scores)):
science_total = science_total + student_scores[r][1]
science_average = science_total / len(student_scores)
print("Science average:", science_average)
# Question 4: Max English, min Math
max_english = student_scores[0][2]
min_math = student_scores[0][0]
for r in range(len(student_scores)):
if student_scores[r][2] > max_english:
max_english = student_scores[r][2]
if student_scores[r][0] < min_math:
min_math = student_scores[r][0]
print("Highest English score:", max_english)
print("Lowest Math score:", min_math)
Example output:
Scores of 90 or above: 5
Student 3 total: 253
Science average: 87.75
Highest English score: 92
Lowest Math score: 76