Python Program to Find Grade from Percentage
Use a Python program with
if-elif-else statements to check the percentage ranges and print the corresponding grade, for example: if percentage >= 90: grade = 'A'.Examples
Input95
OutputGrade: A
Input76
OutputGrade: C
Input45
OutputGrade: F
How to Think About It
To find the grade from a percentage, compare the percentage value against fixed ranges using
if and elif conditions. Assign a grade based on which range the percentage falls into, such as 90 and above for 'A', 80 to 89 for 'B', and so on.Algorithm
1
Get the percentage input from the user.2
Check if the percentage is 90 or above, assign grade 'A'.3
Else if percentage is 80 or above, assign grade 'B'.4
Else if percentage is 70 or above, assign grade 'C'.5
Else if percentage is 60 or above, assign grade 'D'.6
Else assign grade 'F'.7
Print the grade.Code
python
percentage = float(input('Enter percentage: ')) if percentage >= 90: grade = 'A' elif percentage >= 80: grade = 'B' elif percentage >= 70: grade = 'C' elif percentage >= 60: grade = 'D' else: grade = 'F' print('Grade:', grade)
Output
Enter percentage: 85
Grade: B
Dry Run
Let's trace the input 85 through the code
1
Input percentage
percentage = 85.0
2
Check if percentage >= 90
85.0 >= 90 is False
3
Check if percentage >= 80
85.0 >= 80 is True, so grade = 'B'
4
Print grade
Output: Grade: B
| Step | Condition | Result | Grade |
|---|---|---|---|
| 1 | Input percentage | 85.0 | |
| 2 | percentage >= 90 | False | |
| 3 | percentage >= 80 | True | B |
| 4 | Print grade | B |
Why This Works
Step 1: Input percentage
We get the percentage value from the user as a number to check.
Step 2: Check ranges
Using if-elif, we compare the percentage to fixed ranges to find the correct grade.
Step 3: Assign grade
Once the correct range is found, we assign the corresponding grade letter.
Step 4: Output grade
Finally, we print the grade so the user can see the result.
Alternative Approaches
Using dictionary with thresholds
python
percentage = float(input('Enter percentage: ')) grades = {90: 'A', 80: 'B', 70: 'C', 60: 'D', 0: 'F'} for threshold in sorted(grades.keys(), reverse=True): if percentage >= threshold: grade = grades[threshold] break print('Grade:', grade)
This method uses a dictionary and a loop to find the grade, making it easier to update thresholds.
Using function with return
python
def find_grade(p): if p >= 90: return 'A' elif p >= 80: return 'B' elif p >= 70: return 'C' elif p >= 60: return 'D' else: return 'F' percentage = float(input('Enter percentage: ')) grade = find_grade(percentage) print('Grade:', grade)
This approach wraps the logic in a function for reuse and cleaner code.
Complexity: O(1) time, O(1) space
Time Complexity
The program uses a fixed number of comparisons regardless of input size, so it runs in constant time O(1).
Space Complexity
Only a few variables are used to store input and grade, so space complexity is O(1).
Which Approach is Fastest?
All approaches run in constant time; using if-elif is simplest, dictionary method is more flexible but slightly more complex.
| Approach | Time | Space | Best For |
|---|---|---|---|
| If-elif-else | O(1) | O(1) | Simple and clear grading logic |
| Dictionary with loop | O(1) | O(n) where n is number of thresholds | Easily update or add grade thresholds |
| Function with return | O(1) | O(1) | Reusable and clean code structure |
Always check percentage ranges from highest to lowest to avoid incorrect grading.
Beginners often check ranges from lowest to highest, causing wrong grades for higher percentages.