🎓 A1Learner
🔍
0 of 116 lessons completed

Python Comparison Operators

A comparison operator is a sign that compares two values and gives True or False. A boolean is a value that is only True or False.

Is it equal?

Two equals signs ask if the values match.

print(5 == 5)
print(5 == 8)

Is it not equal?

The sign != asks if the values are different.

print(5 != 8)
print(9 != 9)

Greater and smaller

These signs work just like the signs in maths.

print(10 > 7)
print(10 < 7)

At least and at most

The small line under the sign lets equal count too.

print(8 >= 8)
print(6 <= 5)

Compare words

Python reads words letter by letter from the left.

print("apple" < "banana")
print("cat" == "cat")

Example

score_a = 82
score_b = 76
print("Same score:", score_a == score_b)
print("Different score:", score_a != score_b)
print("A is higher:", score_a > score_b)
print("A is lower:", score_a < score_b)
print("A at least 80:", score_a >= 80)
print("B at most 70:", score_b <= 70)

Key points

  • Comparison operator: a sign that compares two values and gives True or False.
  • Use == to ask if two values are equal.
  • Use != to ask if two values are not equal.
  • Use > and < for greater than and less than.
  • Use >= and <= when equal also counts as true.
  • Text compares letter by letter, so words in the same case follow alphabet order.
  • Do not mix up = and ==.

In this example, score_a is 82 and score_b is 76. Python checks if the scores are the same and gets False. It checks if they are different and gets True. The last four lines test higher, lower, at least, and at most.

Note

= stores a value.

== compares two values.

Try it Yourself

Edit the code, then press Run.

Running is simulated · Python Playground

Exercise

What does 7 >= 7 give?