Python for Beginners: Learn to Code from Zero · Lesson 10 of 10

Lesson 10: Mini Project: A Marks Report and What to Learn Next

Combine lists, loops, functions and dictionaries in a class marks report, then see where to go next.

Lesson 10: Mini Project: A Marks Report and What to Learn Next

Time to use everything you have learned. We will build a small class marks report: it takes students' marks, gives each a grade, and prints the class average and the topper.

Step 1: the data

A list of dictionaries holds the students.

students = [
    {"name": "Sita", "marks": 72},
    {"name": "Ram", "marks": 85},
    {"name": "Gita", "marks": 38},
    {"name": "Hari", "marks": 91},
]

Step 2: the full program

def grade(marks):
    if marks >= 80:
        return "Distinction"
    elif marks >= 60:
        return "First division"
    elif marks >= 40:
        return "Pass"
    return "Fail"

students = [
    {"name": "Sita", "marks": 72},
    {"name": "Ram", "marks": 85},
    {"name": "Gita", "marks": 38},
    {"name": "Hari", "marks": 91},
]

total = 0
topper = students[0]
for s in students:
    print(f"{s['name']:<6} {s['marks']:>3}  {grade(s['marks'])}")
    total += s["marks"]
    if s["marks"] > topper["marks"]:
        topper = s

print("Average:", total / len(students))
print("Topper:", topper["name"])

Output:

Sita    72  First division
Ram     85  Distinction
Gita    38  Fail
Hari    91  Distinction
Average: 71.5
Topper: Hari

Read it slowly: a function for grades, a list of dictionaries, a loop that prints and adds up, and an if that tracks the best student. The :<6 and :>3 parts just line up the columns.

Make it your own

  • Add a count of how many students passed.
  • Read the marks from a file instead of typing them in.
  • Ask the user for the marks with input() in a loop.
  • Save the final report to a text file.

Where to go next

  • Practise daily. Twenty minutes every day beats three hours once a week.
  • Learn more Python: modules and packages, classes, and working with data.
  • Pick a direction: web development (with Django or Flask), data analysis, automation scripts, or artificial intelligence.
  • Take our other Mero Courses to learn JavaScript and the basics of AI.

Congratulations, you have finished the course. Keep building small programs, because that is how programmers learn.

Finished this lesson?Log in to save your progress and pick up where you left off.
Back to the course

Comments

Loading comments…

MeroSir Team
A free MeroSir course. Run the examples yourself and try the exercises: you learn programming by doing. Spotted a mistake? Use the Report button above the lesson.
Lesson 10: Mini Project: A Marks Report and What to Learn Next | MeroSir