Asking questions
To make decisions, a program first needs to ask questions like “is the age at least 18?”. We do that with comparison operators. The answer is always True or False.
| Operator | Question | Example | Result |
|---|---|---|---|
== | equal to? | 5 == 5 | True |
!= | not equal to? | 5 != 3 | True |
> | greater than? | 5 > 8 | False |
< | less than? | 5 < 8 | True |
>= | greater than or equal to? | 5 >= 5 | True |
<= | less than or equal to? | 9 <= 5 | False |
marks = 72
print(marks >= 40)
print(marks == 100)
print(marks != 0)Output
True False True
Comparing text
Strings can be compared too. Capital letters matter:
password = "Secret123"
print(password == "Secret123")
print(password == "secret123")
print("apple" < "banana")Output
True False True
"apple" < "banana" is True because a comes before b in the alphabet.
Common mistake: Using
=when you mean==. One equals sign stores a value; two equals signs compare.