Chapter 2: Comparison operators

Comparing literals

face Josiah Wang Oana Cocarascu

Now, try these out yourself!

What do you think the output of the following boolean expressions is? Try to guess the answer, and use the system to check your answer. Then test them out on a Python interactive prompt yourself if you are not convinced!

Warning - some of these are trick questions!! 😈 Some of the quirks are specific to the Python language. They may behave differently in another language.

Make sure you understand them to ensure that you never fall into Python’s evil traps! 🐍

As usual, be curious. If you find yourself wondering about some new case, try it out yourself on an interactive prompt to see what pops up, and try to understand why!

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

Question 1

What is the value of the boolean expression below?

3 > 6

False

Question 2

What is the value of the boolean expression below?

2 <= 2

True

Question 3

What is the value of the boolean expression below?

88 == + 88

True
Explanation:

Both are the int 88.

Question 4

What is the value of the boolean expression below?

1+2 == 3

True

Question 5

What is the value of the boolean expression below?

7 != 3 + 4

False

Question 6

What is the value of the boolean expression below?

4.2 < 2.44

False

Question 7

What is the value of the boolean expression below?

3.3 <= 3.3

True

Question 8

What is the value of the boolean expression below?

9.0 > 8.9999

True
Explanation:

The value of 9 is still more than 8.99999, no matter how close they are.

Question 9

What is the value of the boolean expression below?

5 == 5.0000

True
Explanation:

We are comparing the values of the two operands, and not the type. So the value of 5 is equal to the value of 5.0, even if they are different objects and of different types.

Question 10

What is the value of the boolean expression below?

6 // 3 == 6.0 / 3.0

True
Explanation:

We are comparing the values of the two expressions. The left hand side results in an int with value 2, the right hand side is a float with value 2.0. The value of 2 is equal to the value of 2.0, so this evaluates to True.

Question 11

What is the value of the boolean expression below?

1.1 + 2.2 == 3.3

False
Explanation:

Try typing the following into an interactive prompt to see why this is False.

>>> print(1.1+2.2) 
>>> print(3.3) 

It is hard for computers to store decimal fractions on the computer (think \frac{1}{3} = 0.3333333333333333333....), so floating points are often just an approximation.

How do you check for 'equality' between two floating point numbers? Easy! As long as the two numbers are "close enough", then we can say they are equal. So if the difference between the two numbers is "small enough", then we consider them equal.

>>> tolerance = 0.000000001
>>> abs((1.1+2.2) - 3.3) < tolerance

If you prefer a higher level of abstraction, try the math.isclose() function in the math module. Read the documentation, and note the relative tolerance value used by default.

Question 12

What is the value of the boolean expression below?

True != False

True

Question 13

What is the value of the boolean expression below?

False < True

True
Explanation:

Think of False as having the value 0 and True having the value 1 when comparing. False == 0 and True == 1 will both evaluate to True (try this yourself!)

Question 14

What is the value of the boolean expression below?

"10" > "2"

False
Explanation:

This is a trick question! You are comparing two strs, not int. Python will compare the "Unicode code point" of the first character of the string on both sides (use ord() to get this value). If they are the same, compare the second character etc. until one of the string ends. In this example, "10" is smaller than "2" because the Unicode code point of the first character in "10" (ord("1") == 49) is smaller than ord("2") == 50. At this point, there is no longer any need to compare the "0" in "10".

Ever wondered why you sometimes find your files on your computer sorted in this weird order below? Now you know!

  • "Picture1.jpg"
  • "Picture10.jpg"
  • "Picture100.jpg"
  • "Picture11.jpg"
  • "Picture2.jpg"

Question 15

What is the value of the boolean expression below?

"10" > 2

Error
Explanation:

Unfortunately, you cannot compare a str to a number with the >, >=, <, <= operators. Check the error message in an interactive prompt to convince yourself!

Question 16

What is the value of the boolean expression below?

"10" == 2

False
Explanation:

Interestingly, you can compare two different types with == and !=. By default, these operators compare whether the two operands are pointing to the same object. These, however, can change depending of the type of the operand, for example when both are numbers. In that case, Python will compare their values rather than whether they point to the same object.

Question 17

What is the value of the boolean expression below?

"10" == 10

False
Explanation:

We have discussed this back in Lesson 1. Remember that Python is a strongly typed language. So the string "10" is considered different from the string 10.

Question 18

What is the value of the boolean expression below?

"bad" < "and"

False
Explanation:

We saw this in an earlier question (question 14) on how Python compares strings one character at a time, based on its unicode code point (computed with ord()). So ord("a") is 97 and ord("b") is 98, so "b" > "a". There is no need to check the remaining characters.

Question 19

What is the value of the boolean expression below?

"Bad" < "and"

True
Explanation:

Like the previous question, Python will first compare the first character of each string. This time, ord("B") is 49 and ord("a") is 97 (uppercase letters usually come before lowercase letters in the unicode code points). So "B" < "a". There is no need to check the remaining characters.

Question 20

What is the value of the boolean expression below?

"end" > "ending" 

False
Explanation:

When the first character is the same on both sides, Python will compare the second character, etc. until one of them is different. If Python has run out of characters on one side and has some extra characters on the other, then the longer string will be the larger one. So "end" < "ending" for this example.