GPA Calculator Implementation Using Python


In today’s article, we will discuss GPA calculation using python programming. We all have either the GPA system or the percentage system in our education grading system to measure a student’s performance in his/her academic session. So, to calculate the overall student performance GPA calculator is used.

What is GPA sytem?

GPA stands for Grade Point Average. It refers to a grading system used to measure students’ performance in an academic session. It is a numerical index that summarizes the academic performance of the students in an academic year.

Different schools and other educational institutes measure GPA on different scales. For example, some measure it on a scale of 1 to 5, and some measure it on a scale of 1 to 10. This number represents the average value of all final grades earned in a course over a specific period of time.

Some organizations also use it to measure a candidate’s employability; apart from performance in the interview, these organizations also consider the GPA in academics.

GPA calculator

Aim of GPA Calculator

So our goal is to make a python script that takes in an array of grades as input, processes these inputs and computes the weighted average and sum, and then outputs the current grade in your class to the Terminal! How does this all work, though? So essentially, the script will accept these inputs as command-line arguments.

GPA Calculator Code in Python

This program is designed to calculate a person’s GPA based on given point values for each letter grade. It is assumed that each class is worth one credit, and thus, all the courses are weighted the same. The given grade point values are 

  1. A = 4.0, 
  2. A- = 3.67, 
  3. for B+ = 3.33, 
  4. B = 3.0, 
  5. B- = 2.67,
  6. for C+ = 2.33, 
  7. C = 2.0,
  8. C- = 1.67, 
  9. D+ = 1.33,
  10.  D = 1.0
  11.  F = 0.0
def gpa_calculator(grades):
    points = 0
    i = 0
    grade_c = {"A":4,"A-":3.67,"B+":3.33,"B":3.0,"B-":2.67, "C+":2.33,"C":2.0,"C-":1.67,"D+":1.33,"D":1.0,"F":0}
    if grades != []:
        for grade in grades:
            points += grade_c[grade]
        gpa = points / len(grades)
        return gpa
        return None 

grades = [ 'A', 'B', 'A', 'C']

grades = ['A', 'B', 'C', 'F', 'A', 'B+']

Explanation of the code

  1. First, we have declared a function name as gpa_calculator with grades as its parameter.
  2. Then, we had declared variables such as points to count the points as grades are entered.
  3. Declared an iterable variable i starting with 0
  4. Given a condition that the list of grades should not be empty. If the grades are not entered, then we will not calculate the GPA. Hence it will return None.
  5. If grades are entered, then we will move to the iteration. The iteration i will start from 0 and will go up to the length of the list. For example, if the grades = [ ‘A’, ‘B’, ‘A’, ‘C’] then the length will be 4 hence the loop will iterate up to 4.
  6. Now, we give the if conditions as mentioned above and the variable points will keep incrementing itself. For example, grades = [ ‘A’, ‘B’, ‘A’, ‘C’] the grade[0] = ‘A’ then the points = 0 +4.0 =4.0 it will get stored into the point variable, similarly grade[1] =’B’ , points = 4.0+3.0 =7.0, grade[2] =’A’ points will now be 7.0+3.0=11.0, grade[3]=’C’ points =11.0+2.0=13.0. Now the iteration stops thus the final value of points becomes 13.0
  7. To calculate the GPA, we have the formula as points/len(grades). In our case, it becomes 13.0/4 = 3.25
  8. Thus we get to print the GPA as 3.25.

Improvements and Future

The above code was just the idea on the calculation of GPA. But our next target is to have a great user interface where users can enter the details by having the GUI.

Create GUI GPA calculator using tkinter

from tkinter import *
import tkinter.messagebox

def gpa_calculator(grades):
    points = 0
    i = 0
    grade_c = {"A":4,"A-":3.67,"B+":3.33,"B":3.0,"B-":2.67, "C+":2.33,"C":2.0,"C-":1.67,"D+":1.33,"D":1.0,"F":0}
    if grades != []:
        for grade in grades:
            if grade not in grade_c:
                return "Invalid"
            points += grade_c[grade]
        gpa = points / len(grades)
        return gpa
        return None

class App:
    def __init__(self, parent):
        self.parent = parent
        self.frame_1 = Frame(parent)
        self.sub_count = 1
        self.subs = []
        Label(self.frame_1, text="Enter Grade :").grid(row=self.sub_count-1, column=0)
        self.subs[self.sub_count-1].grid(row=self.sub_count-1, column=1)

        self.btn_1 = Button(parent, text="Add Courses !",
        self.btn_2 = Button(parent, text="Calculate GPA",

    def add_courses(self):
        self.sub_count += 1
        Label(self.frame_1, text="Enter Grade :").grid(row=self.sub_count-1, column=0)
        self.subs[self.sub_count-1].grid(row=self.sub_count-1, column=1)

    def calc_CG(self):
        grades = []
        for sub in self.subs:
            if sub.get() != "":
        tkinter.messagebox.showinfo("Predicted CGPA ", str(gpa_calculator(grades)))

root = Tk()
app = App(root)


See, Also


It’s really nice to have a double-check sort of a tool with you to see where you are at in your class! Even if you haven’t completed or received grades yet, you could always input your grades to assignments you have completed so far, compute the weights depending on your class, and get an output!

This was so much fun, and making these little Python scripts are just the best way to get hands-on experience with Python!

If you are having any doubts or confusion feel free to comment down below. Till then keep pythoning geeks!

Notify of
Newest Most Voted
Inline Feedbacks
View all comments
2 months ago

Hi there. The first code listing doesn’t appear to be wrong per se, but it’s really quite unpythonic and shouldn’t be held up as an example to new Python programmers. In the first place, the ‘for’ loop should not index into the ‘grades’ list but rather should iterate over the list directly, such as “for grade in grades:” or similar. Secondly, there is far too much repetition in the for loop. You could easily pre-populate a dictionary which maps letter grades to points (e.g. call it “point_map”) and access that dictionary on each iteration of the loop, giving you something like “points += point_map[grade]” and thereby collapsing twenty-something lines into one.