Making decisions in Python

Module 3 · Making decisions in Python · Lesson 2 of 7

Comparison operators

Python gives you six operators for comparing values. Each one asks a question about two values and answers with a True or a False.

The six comparison operators
==equal to5 == 5True
!=not equal to5 != 3True
>greater than5 > 3True
<less than5 < 3False
>=greater or equal5 >= 5True
<=less or equal3 <= 5True

⚠ Don't confuse them: == asks “are these equal?”, while a single = assigns a value. Mixing them up is a classic bug.

Most read exactly as they look. The two to watch are == (“equal to”, a double equals) and != (“not equal to”). The double equals matters: a single = is the assignment you already know — it stores a value — whereas == compares two. Reach for the wrong one and your condition won't mean what you think.

Run it yourselfruns in your browser · Python
main.py

They aren't just for numbers — you can compare strings too, and even the results of other expressions. Whatever you compare, the answer is always a boolean, which is exactly what we'll start putting to work.