2. Basic Types: Numbers
Comparisons
In addition to operators for standard arithmetic, Python also provides operators meant for comparisions. Each of these operators also has their mathematical equivalent. In mathematics we use #=# to test for equality, in Python the #\color{#3e999f} {\mathtt{\text{=}}}# is reserved for variable assignment, so #\color{#3e999f} {\mathtt{\text{==}}}# is used for equality instead. All comparison operators are used to compare two values or objects.
Operator | Name | Equivalent | Expression | Returns |
#\mathtt{\text{==}}# | Equal | #x = y# | #\mathtt{\text{x}}# #\mathtt{\text{==}}# #\mathtt{\text{y}}# | #\color{#F5871F} { \mathtt{\text{True}} }# if #\mathtt{\text{x}}# is equal to #\mathtt{\text{y}}#, otherwise #\color{#F5871F} {\mathtt{\text{False}}}#. |
#\mathtt{\text{!=}}# | Not equal | #x \neq y# | #\mathtt{\text{x}}# #\mathtt{\text{!=}}# #\mathtt{\text{y}}# | #\color{#F5871F} { \mathtt{\text{True}} }# if #\mathtt{\text{x}}# is not equal to #\mathtt{\text{y}}#, otherwise #\color{#F5871F} {\mathtt{\text{False}}}#. |
#\mathtt{\text{>}}# | Greater than | #x \gt y# | #\mathtt{\text{x}}# #\mathtt{\text{>}}# #\mathtt{\text{y}}# | #\color{#F5871F} { \mathtt{\text{True}} }# if #\mathtt{\text{x}}# is greater than #\mathtt{\text{y}}#, otherwise #\color{#F5871F} {\mathtt{\text{False}}}#. |
#\mathtt{\text{<}}# | Less than | #x \lt y# | #\mathtt{\text{x}}# #\mathtt{\text{<}}# #\mathtt{\text{y}}# | #\color{#F5871F} { \mathtt{\text{True}} }# if #\mathtt{\text{x}}# is less than #\mathtt{\text{y}}#, otherwise #\color{#F5871F} {\mathtt{\text{False}}}#. |
#\mathtt{\text{>=}}# | Greater than or equal to |
#x \geq y# | #\mathtt{\text{x}}# #\mathtt{\text{>=}}# #\mathtt{\text{y}}# | #\color{#F5871F} { \mathtt{\text{True}} }# if #\mathtt{\text{x}}# is greater than or equal to #\mathtt{\text{y}}#, otherwise #\color{#F5871F} {\mathtt{\text{False}}}#. |
#\mathtt{\text{<=}}# | Less than or equal to |
#x \leq y# | #\mathtt{\text{x}}# #\mathtt{\text{<=}}# #\mathtt{\text{y}}# | #\color{#F5871F} { \mathtt{\text{True}} }# if #\mathtt{\text{x}}# is less than or equal to #\mathtt{\text{y}}#, otherwise #\color{#F5871F} {\mathtt{\text{False}}}#. |
The comparison expressions return either #\color{#F5871F} { \mathtt{\text{True}} }# or #\color{#F5871F} { \mathtt{\text{False}} }# when the expression is true or untrue. Let's take a look at some examples.
Comparison examplesSimply test the value of a variable:
>>> var = 1 + 1
>>> var == 1 |
False
|
>>> var == 2
|
True
|
But, we can use #\color{#3e999f} {\mathtt{\text{!=}}}# to check if #\mathtt{\text{var}}# is not equal to #\color{#F5871F} {\mathtt{\text{1}}}#. This is the same as testing the negation of equality.
>>> var != 1
|
True
|
>>> not var == 1
|
True
|
It is important to realize that #\color{#3e999f} {\mathtt{\text{==}}}# does not test for type equality, which means that objects of type #\color{#4271ae} {\mathtt{\text{int}}}# and #\color{#4271ae} {\mathtt{\text{float}}}# representing the same value are equal.
>>> 3 == 3.0
|
True
|
However, it is crucial to note that different expressions containing different combinations of #\color{#4271ae} {\mathtt{\text{int}}}# or #\color{#4271ae} {\mathtt{\text{float}}}# do not necessarily return the same value, even if they should. This is a common occurence in any programming language, and the result of the so called floating point error, which can result in unexpected behaviour.
>>> 1 + 2 == (0.1 + 0.2) * 10
|
False
|
>>> 1 + 2
|
3
|
>>> (0.1 + 0.2) * 10
|
3.0000000000000004
|
Floating point errors are the result of underlying floating point formats at the lowest level of computer memory only being able to achieve limited precision.
Or visit omptest.org if jou are taking an OMPT exam.