Skip to main content
  • Python is available now. All other compilers are coming soon.

    Python
  • Popular Tutorials

    • Getting Started With Python
    • Python if Statement
    • while Loop in Python
    • Python Lists
    • Dictionaries in Python
    All Python Tutorials

    Reference Materials

    Built-in FunctionsList MethodsDictionary MethodsString Methods
    View all

Online Compilers

  • Python Compiler
  • Python Playground
  • Python Projects
  • Exercises

Tutorials

  • Learn Python
  • Python for Beginners
  • Variables and Types
  • Python Strings
  • Python Lists
  • Python If Else
  • Python For Loops
  • Python Functions
  • Python Classes

Examples

  • Python Examples
  • Hello World
  • Add Two Numbers
  • Even or Odd
  • Factorial
  • Fibonacci
  • Prime Check
  • Palindrome Check

Company

  • About
  • Contact
  • Privacy Policy
  • Terms of Use
  • Disclaimer

© 2026 ILoveCompiler. All rights reserved.

  1. Home
  2. Python Examples
  3. Grade Calculator

Conditional Statements · Python example

Grade Calculator

Map a numeric score to a letter grade using an if-elif ladder.

Open the compilerAll examples
beginnerif elifrangesscores

An if-elif ladder checks conditions in order and stops at the first match. This is a natural fit for grade boundaries.

example.py
score = 87

if score >= 90:
    grade = "A"
elif score >= 80:
    grade = "B"
elif score >= 70:
    grade = "C"
elif score >= 60:
    grade = "D"
else:
    grade = "F"

print(f"Score: {score}")
print(f"Grade: {grade}")

How the code works

  • 87 >= 90 is False, so Python continues.
  • 87 >= 80 is True, so grade becomes B and the remaining branches are skipped.
  • The score and grade are printed.

Key ideas

  • Each condition covers the range below the previous boundary.
  • The first True condition wins, so order matters.
  • The final else handles every remaining score.

Try changing it

  • Read the score and validate that it is between 0 and 100.
  • Add plus and minus grades.
  • Use a list of (minimum, grade) pairs in a loop.

Related examples

beginner

Find the Largest of Three Numbers

Combine conditions with and to find the maximum of three values.

beginner

Check a Leap Year

Combine divisibility rules to decide whether a year is a leap year.

Previous exampleCheck a Leap YearNext exampleMultiplication Table

Expected output

Score: 87
Grade: B

Output is compared after trimming trailing whitespace on each line. Run the code to see it produced by CPython in your browser.

Practice next

Apply the same idea with automatic tests or browse a guided project.

ExercisesProjects