Search⌘ K
AI Features

Solution: Class Grades

Explore how to use Python dictionaries to store student names and scores and leverage loops and functions to display this data clearly. This lesson helps you understand constructing reusable code structures for handling related data efficiently.

We'll cover the following...

This program stores a list of students with their scores and then prints each student’s name and score.

  • students is a list containing dictionaries.

    • Each dictionary represents one student with two pieces of information:

      • "name" → the student’s name

      • "score" → their test score

  • The for loop goes through each student dictionary one by one.

  • Inside the loop:

    • student['name'] gets the student’s name.

    • student['score'] gets their score.

  • The print() statement displays both pieces of data in one sentence.

Python
students = [
{"name": "Sam", "score": 87},
{"name": "Luna", "score": 92},
{"name": "Kai", "score": 78}
]
for student in students:
print(student['name'], "scored", student['score'])