Comparison Operators
In this lecture we will be learning about Comparison Operators in Python. These operators will
allow us to compare variables and output a Boolean value (True or False).
If you have any sort of background in Math, these operators should be very straight forward.
First we'll present a table of the comparison operators and then work through some examples:
Table of Comparison Operators
In the table below a= 3 and b=4
Operator Description Example
If the values of two operands are equal, then
== (a == b) is not true.
the condition becomes true.
If values of two operands are not equal, then
!= (a != b) is true
condition becomes true.
If the value of left operand is greater than the
> value of right operand, then condition becomes (a > b) is not true.
true.
If the value of left operand is less than the value
< (a < b) is true.
of right operand, then condition becomes true.
If the value of left operand is greater than or
>= equal to the value of right operand, then (a >= b) is not true.
condition becomes true.
If the value of left operand is less than or equal
<= to the value of right operand, then condition (a <= b) is true.
becomes true.
Let's now work through quick examples of each of these.
Equa
In [1]: 2 == 2
True
Out[1]:
In [2]: 1 == 0
False
Out[2]:
Note that == is a comparison operator, while = is an assignment operator.
Not Equal
In [3]: 2 != 1
True
Out[3]:
In [4]: 2 != 2
False
Out[4]:
Greater Than
In [5]: 2 > 1
True
Out[5]:
In [6]: 2 > 4
False
Out[6]:
Less Than
In [7]: 2 < 4
True
Out[7]:
In [8]: 2 < 1
False
Out[8]:
Greater Than or Equal to
In [9]: 2 >= 2
True
Out[9]:
In [10]: 2 >= 1
True
Out[10]:
Less than or Equal to
In [11]: 2 <= 2
True
Out[11]:
In [12]: 2 <= 4
True
Out[12]:
Great! Go over each comparison operator to make sure you understand what each one is saying.
But hopefully this was straightforward for you.
Next we will cover chained comparison operators
Chained Comparison Operators
An interesting feature of Python is the ability to chain multiple comparisons to perform a more
complex test. You can use these chained comparisons as shorthand for larger Boolean
Expressions.
In this lecture we will learn how to chain comparison operators and we will also introduce two
other important statements in Python: and and or .
Let's look at a few examples of using chains:
In [13]: 1 < 2 < 3
True
Out[13]:
The above statement checks if 1 was less than 2 and if 2 was less than 3. We could have written
this using an and statement in Python:
In [14]: 1<2 and 2<3
True
Out[14]:
The and is used to make sure two checks have to be true in order for the total check to be true.
Let's see another example:
In [15]: 1 < 3 > 2
True
Out[15]:
The above checks if 3 is larger than both of the other numbers, so you could use and to rewrite it
as:
In [16]: 1<3 and 3>2
True
Out[16]:
It's important to note that Python is checking both instances of the comparisons. We can also use
or to write comparisons in Python. For example:
In [17]: 1==2 or 2<3
True
Out[17]:
Note how it was true; this is because with the or operator, we only need one or the other to be
true. Let's see one more example to drive this home:
In [18]: 1==1 or 100==1
True
Out[18]:
Great! For an overview of this quick lesson: You should have a comfortable understanding of using
and and or statements as well as reading chained comparison code.
Go ahead and go to the quiz for this section to check your understanding!
Thank You