Comparison Operators
A comparison takes 2 values and produces a bool, which is either True or False. That result is a value like any other: you can print it, store it in a variable, or feed it into a larger expression. Later lessons use it to steer which lines run; this one makes sure you can predict the result before it drives anything.
| Operator | Question asked | Example | Result |
|---|---|---|---|
| `==` | equal value? | `5 == 5.0` | `True` |
| `!=` | different value? | `5 != 5` | `False` |
| `<` | less than? | `3 < 7` | `True` |
| `>` | greater than? | `3 > 7` | `False` |
| `<=` | less than or equal? | `7 <= 7` | `True` |
| `>=` | greater than or equal? | `6 >= 7` | `False` |
stock = 4
requested = 5
enough = stock >= requested
print(enough)
print(type(enough).__name__)
print(stock == requested, stock != requested)Output:
False bool False True
enough holds False, and the variable name reads as the question it answers. Naming a comparison result this way pays off whenever the same condition would otherwise appear twice. == and != are exact opposites: for any pair of values, exactly one of them is True.
Numbers compare by value
5 == 5.0 is True because Python compares the numeric value, and the int 5 and the float 5.0 stand for the same quantity. True == 1 is also True, since bool is a kind of int, with True worth 1 and False worth 0. A string of digits plays by other rules: "5" == 5 is False with no error, because a string and a number can never be equal, however alike they look when printed.
Strings compare character by character
print("apple" < "banana")
print("apple" < "apples")
print("10" < "9")
print("Zebra" < "apple")
print("1" == 1)Output:
True True True True False
Python walks both strings from the left and decides at the first position where they differ. "apple" is less than "banana" because "a" comes before "b" at the first position. "apple" runs out before "apples" does, and a shorter string that matches so far counts as smaller. "10" < "9" is True because the first characters are "1" and "9", and the second character is never consulted. Every character has a numeric code, and uppercase letters have smaller codes than lowercase ones, which is why "Zebra" sorts before "apple".
- 48
- 65
- 97
Ordering a string against a number goes further than False: "10" < 9 raises TypeError: '<' not supported between instances of 'str' and 'int'. That ordering has no sensible answer, so Python stops instead of guessing. Equality behaves differently: "1" == 1 quietly gives False, because asking whether 2 things are the same is always answerable, and for different types the answer is no. Both outcomes point to one habit: convert the text with int() before comparing it to a number.
Comparing after converting
entered = "100"
limit = "90"
print(entered > limit)
print(int(entered) > int(limit))Output:
False True
As text, "100" loses to "90" because "1" comes before "9". As numbers, 100 wins. Scores that were never converted will order 100 before 11 and before 2, and the bug shows up as results that look almost right, which is the hardest kind to notice. The conversion is one call per side, and it turns a question about characters into a question about quantities.
Testing 2 floats for exact equality has its own pitfalls, which a later lesson covers. Comparisons between ints, between strings, and between an int and a float are safe to reason about today.
- Do
- Convert text with int() or float() before comparing it to a number
- Store a comparison in a well named variable when you use it twice
- Expect int and float to compare by numeric value
- Avoid
- Expect "5" == 5 to be True; it's False with no warning
- Order a str against an int; that raises TypeError
- Rely on text ordering for numbers held as strings
What does this program print?
a = "7"
b = 7
print(a == b, str(b) == a, int(a) == b)- False True True
- True True True
- False False True
- A TypeError traceback
Asking whether a shelf covers an order means asking whether the stock is at least the amount wanted, which is >=. When one side arrives as text, convert it first, because an ordering operator between a str and an int raises before it produces any answer.
About this lesson
This section is part of the Python Expressions: Intermediate lesson on DataDriven, the home of data engineering. Lessons cover SQL, Python, Spark, data modeling and pipeline architecture, and each section pairs its explanation with worked examples and practice you run as you go.