Python Program to Calculate the Grade of a Student

1. Introduction

In this tutorial, we will learn how to write a Python program to calculate the grade of a student.

Calculating a student's grade based on their marks is a standard operation in educational systems. This process involves assessing individual marks in various subjects, calculating the total and average, and then assigning a grade according to predefined criteria. In Python, this can be automated to handle multiple students or subjects efficiently.

2. Program Steps

1. Define the marks obtained by the student in each subject.

2. Calculate the total and average of these marks.

3. Determine the grade based on the average using a series of conditions.

4. Print the student's marks, average, and grade.

3. Code Program

# Function to calculate total, average, and grade
def calculate_grade(marks):
    # Calculate the total marks
    total = sum(marks)
    # Calculate the average marks
    average = total / len(marks)
    # Determine the grade based on the average
    if average >= 90:
        grade = 'A'
    elif average >= 80:
        grade = 'B'
    elif average >= 70:
        grade = 'C'
    elif average >= 60:
        grade = 'D'
        grade = 'F'
    # Return a tuple of total, average, and grade
    return total, average, grade

# Marks obtained by the student
student_marks = [85, 92, 78, 90, 88]
# Calculate the grade
total, average, grade = calculate_grade(student_marks)
# Print the results
print(f"Total Marks: {total}")
print(f"Average Marks: {average:.2f}")
print(f"Grade: {grade}")


Total Marks: 433
Average Marks: 86.60
Grade: B


1. The function calculate_grade takes a list marks containing the marks obtained by a student.

2. total is computed using the sum function, which adds up all the elements in marks.

3. average is calculated by dividing total by the number of subjects, obtained using len(marks).

4. The student's grade is determined using a series of if-elif-else statements, comparing average against the grade boundaries.

5. The function returns a tuple containing total, average, and grade.

6. The student_marks list is defined with the marks of the student.

7. The calculate_grade function is called with student_marks as the argument, and the results are stored in total, average, and grade.

8. The print statements use f-strings to format and display the total marks, average (formatted to two decimal places), and grade of the student.

9. The output reflects the total marks as 433, the average as 86.60, and the corresponding grade as B for the student's performance.