Comparison operators
Contents
Comparison operators#
Comparison operators are used to compare values using mathematical logic and return a single Boolean value of either True
or False
. We will see later that they are important for counting, extracting and modifying data. For example, counting the number of students with GPA higher than a given threshold requires a comparison, for each student, of their GPA to the threshold.
In the cell below, Python compares two integers (is 5 strictly smaller than 7?) and returns the Boolean that reflects the comparison.
5<7
True
In the expression above, Python followed mathematical logic and declared the comparison to be true.
Below is a table of comparison operators.
Operator |
What it means |
---|---|
== |
Equal |
!= |
Not equal |
< |
Less than |
<= |
Less or equal |
> |
Greater than |
>= |
Greater or equal |
To better understand how these operators work, we illustrate their use in the cell below where we compare two real numbers.
x = 1.5
y = 0.8
print("x == y:", x == y)
print("x != y:", x != y)
print("x < y:", x < y)
print("x <= y:", x <= y)
print("x > y:", x > y)
print("x >= y:", x >= y)
x == y: False
x != y: True
x < y: False
x <= y: False
x > y: True
x >= y: True
We showed examples of comparisons of integers and floats, but strings can also be used with comparison operators.
a='Dan'
b='Mike'
print("a == b:", a == b)
print("a != b:", a != b)
print("a < b:", a < b)
print("a <= b:", a <= b)
print("a > b:", a > b)
print("a >= b:", a >= b)
a == b: False
a != b: True
a < b: True
a <= b: True
a > b: False
a >= b: False
As you can see, Python compared the two strings and found them different, but is also able to use inequality operators to compare them. The order is determined lexicographically using the ASCII values of the characters.
Letter case is important for comparisons:
'Dan'=='dan'
False
Logical (boolean) operators#
Python’s logical operators perform Boolean arithmetic on one or two inputs and return either True
or False
. The table below shows three logical operators.
Operator |
What it means |
Examples (that return True) |
---|---|---|
and |
True if both are true |
(7>5) and (1.2!=1.3) |
or |
True if at least one is true |
(7<5) or (1.2!=1.3) |
not |
True if input is false |
not(7==5) |
print((7>5) and (1.2!=1.3))
print((7>5) & (1.2!=1.3))
print((7<5) or (1.2!=1.3))
print((7<5) | (1.2!=1.3))
True
True
True
True
Note: You will see in some code examples here and elsewhere the logic operators and
and or
replaced by something called bitwise operators: &
used instead of and
and |
instead of or
.
Note that, even if they give the same results when applied to boolean data types (as in the examples above), they have different meanings for other data types. We will use mostly and
and or
in this textbook.
Finally, an expression can contain multiple comparisons, and all comparisons must hold in order for the whole expression to be True. You can see a couple of examples below. In the first, Python evaluates first the expression 5-2
(equal to 3
), then evaluates 1.2 <= 1.8
and 1.8 < 3
(both True), and finally use the boolean operator and
to evaluate the full expression.
1.2 <= 1.8 < 5-2
True
'Dan' != 'Mike' == 'Nike'
False
Truth tables#
An easy way to summarize the outcome of logic operators are truth tables, and we show them below for and
and or
. Note that we used them in the multiple comparisons above. They are useful for algorithmic thinking in programming. For simplicity we summarize them in a single table.
a |
b |
a and b |
a or b |
---|---|---|---|
True |
True |
True |
True |
True |
False |
False |
True |
False |
True |
False |
True |
False |
False |
False |
False |