Compare numbers, strings, and other values using simple expressions
Comparison operators compare two values and return a Boolean: True
or False
.
Operator | Description | Example |
---|---|---|
== | Equal to | 5 == 5 → True |
!= | Not equal to | 5 != 3 → True |
> | Greater than | 7 > 2 → True |
< | Less than | 4 < 6 → True |
>= | Greater than or equal to | 5 >= 5 → True |
<= | Less than or equal to | 3 <= 4 → True |
x = 10
y = 7
print(x == y) # False
print(x != y) # True
print(x > y) # True
print(x < y) # False
print(x >= y) # True
print(x <= y) # False
Strings are compared using lexicographic (dictionary) order.
a = "apple"
b = "banana"
print(a == b) # False
print(a < b) # True ('apple' comes before 'banana')
score = 85
if score >= 90:
print("Grade: A")
elif score >= 80:
print("Grade: B")
else:
print("Grade: C or below")
==
instead of =
when checking equality.18 < age < 65
are allowed in Python.