Comparison operators let you ask questions about values.
They return either True or False.
| Operator | Meaning | Example result |
|---|---|---|
< |
less than | 6 < 2 ➜ False |
<= |
less than or equal | 6 <= 6 ➜ True |
> |
greater than | 6 > 2 ➜ True |
>= |
greater than or equal | 6 >= 8 ➜ False |
== |
equal | 6 == 6 ➜ True |
!= |
not equal | 6 != 6 ➜ False |
Strings compare by Unicode code points, letter by letter:
print("Cello" > "Brass") # True because "C" (67) > "B" (66)
print("Cello" == "cello") # False uppercase C ≠ lowercase cCheck code points with ord():
print(ord("a")) # 97
print(ord("A")) # 65a = 10
b = 7
print(a > b) # True
print(a == "10") # False (different types)
print("cat" < "dog") # True
print("10" > "2") # False ("1" comes before "2")-
Create a file called
Task_10.py. -
Set
x = 4andy = 9. -
Print the results of these comparisons on separate lines:
x == yx != yx < yx >= y
-
Define
word1 = "Alpha"andword2 = "alpha". Compare them with==,<, and>, printing each result. -
Use
ord()to print the Unicode code point of the first character in bothword1andword2. -
Add one line that shows whether
x * 2is equal toy - 1.
Run the script and verify the printed booleans match your expectations.