Lesson 02-06: Comparison Operators¶

Learning Target: I can use boolean operators to compare values.

Comparison Operators¶

Now that we’ve famliarized ourselves with the basic boolean operators, let’s dive a little bit deeper. These next set of boolean operators will enable us to compare values. These comparison operators are:

Description Operator
Equal To ==
Not Equal To !=
Greater Than >
Less Than <
Greater Than or Equal to >=
Less Than or Equal to <=

With the exception of == and !=, these operators should seem fairly familiar - you probably encountered them in your math class when discussing inequalities.

You may be wondering why the “equals to” is two equal signs instead of one. Pop quiz!

ch0206-1: What does a single equal sign mean in Python?
• (A) equals to
• Remember - equals to is a double equal sign!
• (B) variable assignment
• Nice job!
• (C) mathematical equation
• Remember to think in the context of python code. When would you see an equal sign?

Recall that a single equal sign indicates that some value is being saved into some variable. Therefore, the computer needs a way to differentiate between variable assignment and “equals to” - so “equals to” is ==.

Note that while you can compare strings using > and <, we will not be covering it in this book.

Examples¶

Basic Examples¶

Try to look through every example and make sure that you understand each one:

Special Examples¶

These examples cover some special cases that may not be thought about at first:

Examples 1 + 2¶

# example 1
print(13.0000 == 13) # true

# example 2
print(13 == "13") # false

In example 1, although the left side is a float and the right side is an integer, it still prints True because their values are compared. Values are compared with floats and ints because they are both numbers. However, as you can see in example 2, the same privilege doesn’t extend to strings. Types are considered when comparing values.

Examples 3 + 4¶

# example 3
print(14 > 10 + 2) # true

# example 4
print("hehe" + "he" == "he" * 3) # true

In both of these examples, the left and/or right side of the operator is an expression. These are still valid because the expressions are evaluated before the boolean operator is handled.

For example, in example 4, both "hehe" + "he" and "he" * 3 are evaluated. "hehe" + "he" becomes "hehehe" and "he" * 3 becomes "hehehe", so the comparison in the end is "hehehe" == "hehehe", which is True!

Example 5¶

# example 5
print(1 < 2 < 3) # true

You can chain together comparisons. This is something unique to python and newer programming languages. The important thing is to know that the chained comparison:

a < b < c
# or
1 < 2 < 3

is the same thing as saying:

a < b and b < c
# or
1 < 2 and 2 < 3

That is to say, chained comparisons are evaluated as separate boolean expressions connected with AND operators.

Example 6¶

# example 6
print(1 == 1 and False) # false

Here, we are combining the comparison operators and boolean operators from before. It is important to know the order in which things are evaluated. In cases like these, please know that comparison operators always get evaluated BEFORE and/or/not.

Checks for Understanding¶

Q#1¶

Drag the comparison operator to its description. Try again, review the lesson if you need to!
• ==
• equal to
• >
• greater than
• <
• less than
• !=
• not equal to
• >=
• greater than or equal to
• <=
• less than or equal to

Q#2¶

Write an expression that tests if the variable x is equivalent to 12.3 Close - what's the equals to operator? Try again!

Q#3¶

Evaluate True or False: 1 == 1.0 Remember - uppercase T in True!

Evaluate True or False: 1 > 1.01 Remember - uppercase F in False!

Evaluate True or False: -1 <= -2 Remember - uppercase F in False!

Evaluate True or False: 3 != "3" Remember - uppercase T in True!

Next Section - Lesson 02-07: If Statements