Comparison Operators
15 min · Beginner
Need help? Ask a tutor on WhatsApp (opens in a new tab)A comparison asks a yes-or-no question about two values and answers with True or False. Python adds chaining, so a range check reads like maths.
The six comparisons
Comparison operator is a symbol such as ==, !=, <, >, <= or >= that compares two values and gives back a boolean, either True or False.
Every condition in a program starts as a comparison. Python gives one operator for each question.
age = 20print(age >= 18)print(age == 25)print(age != 30)print(5 == 5.0)print("5" == 5)A single = stores a value; == asks if two values are equal. 5 == 5.0 is True because the numbers match, but "5" == 5 is False: Python never turns text into a number for you.
Chained comparisons
Chained comparison is two or more comparisons written in a row, such as a < b < c. It is true only when every link is true, and the middle values are worked out just once.
To test that a value lies in a range, other languages join two checks. Python lets you write the range once.
score = 85print(70 <= score <= 90)print(90 <= score <= 100)print(10 < 20 < 15)Example 1 · Why money checks avoid ==
total = 0.1 + 0.2print(total == 0.3)print(total)print(abs(total - 0.3) < 0.000001)False 0.30000000000000004 True
What this code does
- Floats are stored in binary, so 0.1 + 0.2 is a tiny bit above 0.3.
abs()gives a number without its sign, so the last line asks if the two are very close.
Example 2 · Marks and grade bands
marks = 82print(60 <= marks < 75)print(75 <= marks < 90)print(marks == 82.0)False True True
What this code does
- Each band uses one chained comparison.
- The bands use
<on the upper end so no mark belongs to two bands. 82 == 82.0isTrue: int and float with the same value are equal.
Python reads 70 <= score <= 90 as 70 <= score and score <= 90, and looks at score only once. The last line is False because the second check fails.
Comparing strings
Text is compared one character at a time, like words in a dictionary, using each character's code number.
print("apple" < "banana")print("apple" == "Apple")print("apple" < "Banana")Capital letters have smaller codes than small letters ("A" is 65, "a" is 97). So "apple" == "Apple" is False, and "apple" < "Banana" is also False.
== versus is
Identity is the question "are these two names the same object?" that is answers. Equality (==) only asks whether two values look the same. Two equal values can still be different objects.
== asks if two values are equal. is asks if two names point to the very same object. Use is for None.
a = "mentor"b = aprint(a is b)print(a == "mentor")value = Noneprint(value is None)print(value is not None)Example 3 · None is not zero
reading = Noneprint(reading is None)
reading = 0print(reading is None)print(reading is not None)True False True
What this code does
Nonemeans "no value yet".- After
reading = 0there is a value, even though it is zero, sois NoneisFalse.
b = a does not copy anything: both names point to one object, so a is b is True. For None always write is None, because there is only one None in the program.
Next step
Check your understanding
9 quick questions