Abhik Sengupta Abhik Sengupta - 10 months ago 58
Python Question

Python - Dictionary with Grade Range

I have a python dictionary with Grade Ranges as shown below:

grade_boundaries = {
'A': [70, 100],
'B': [60, 69],
'C': [50, 59],
'D': [40, 49],
'E': [30, 39],
'F': [0, 29],


I am fairly new to Python and was wondering, how I can read this dictionary such that I can extract the lower boundary as well as the upper boundary for each grade while comparing an input number. I am able to handle dictionary when there is a single value for the key, but in this scenario, I am not sure how to write the code.

Answer Source
if value >= grade_boundaries['A'][0] and value <= grade_boundaries['A'][1]:
    print("value is in grade A")

If you wish, you may extract both boundaries at the same time too:

(lower_bound, upper_bound) = grade_boundaries['A']

If you want to compare all at once:

for grade in grade_boundaries:
    if value >= grade_boundaries[grade][0] and value <= grade_boundaries[grade][1]:
        print("value is in grade " + grade)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download