Lesson 3 of 21 · 2 min
Comparison Operators — Asking Yes or No Questions
The WHY — Why Do We Need Comparison Operators?
Before a program can make a decision, it needs to check something. Comparison operators are how Python checks if things are equal, greater, less, etc. They always return True or False — just like a yes/no answer.
| Operator | Meaning | Example | Result |
|---|---|---|---|
== | Equal to | 5 == 5 | True |
!= | Not equal to | 5 != 3 | True |
> | Greater than | 7 > 3 | True |
< | Less than | 3 < 7 | True |
>= | Greater than or equal | 5 >= 5 | True |
<= | Less than or equal | 4 <= 3 | False |
Seeing Comparison Results
age = 20 # store the number 20 in a variable called age print(age == 20) # is age equal to 20? (double equals for comparison) print(age == 18) # is age equal to 18? print(age != 18) # is age NOT equal to 18? print(age > 18) # is age greater than 18? print(age < 18) # is age less than 18? print(age >= 20) # is age greater than or equal to 20? print(age <= 19) # is age less than or equal to 19?
Output: True False True True False True False
Critical note: == (double equals) checks if two things are equal. = (single equals) assigns a value to a variable. Mixing these up is the #1 beginner mistake.
