Comparing Values

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.

OperatorQuestionExampleResult
==equal to?5 == 5True
!=not equal to?5 != 3True
>greater than?5 > 8False
<less than?5 < 8True
>=greater than or equal to?5 >= 5True
<=less than or equal to?9 <= 5False
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.

HOME LEARN COMMUNITY DASHBOARD