Skip to content

Booleans and comparisons

~15 min ยท prerequisite: Reading error messages

What you will be able to do

  • Write comparisons with ==, !=, <, <=, >, and >=, and predict whether each one gives True or False.
  • Combine two conditions with and, or, and not.
  • Spot the mistakes that make a comparison fail with an error, or pass when it should not.

The idea

A comparison asks a question about two values. The answer is always a boolean, which is True or False. The expression 3 < 5 asks "is 3 less than 5?" and gives True. The expression 3 > 5 gives False.

The six comparison operators are:

  • == checks that two values are equal.
  • != checks that two values are not equal.
  • < and > check less than and greater than.
  • <= and >= check less than or equal to, and greater than or equal to.

Remember the difference between = and ==. As you saw in Variables and types, = stores a value and == compares two values.

A condition is any expression that gives True or False. The next two lessons use conditions to decide what a program does and how often it repeats.

You can combine booleans with three words. Python calls them logical operators:

  • and gives True only when both sides are True.
  • or gives True when at least one side is True.
  • not turns True into False, and False into True.

Python evaluates and and or from left to right and stops as soon as the result is known. This is called short-circuit evaluation. If the left side of and is False, the whole expression is False, so Python never evaluates the right side. This matters when the right side would cause an error.

Python also accepts a chained comparison such as 0 <= age < 18. It means the same as 0 <= age and age < 18.

Strings can be compared with == too. The check "yes" == "yes" gives True, and "Yes" == "yes" gives False, because capital letters count as different characters.

Worked example

score = 72
passed = score >= 50
print(passed)
print(score == 100, score != 100)
print(not passed)

Line by line, this is what Python does:

  1. score = 72 stores the integer 72 under the name score.
  2. passed = score >= 50 checks whether 72 is at least 50. It is, so the result is True, and the name passed stores that boolean.
  3. print(passed) shows True. Python writes booleans with a capital letter.
  4. print(score == 100, score != 100) evaluates two comparisons, which give False and True. The output is False True.
  5. print(not passed) flips True to False, so the output is False.

The full output is:

True
False True
False

Try it

Each block checks its results with assert. If a check is wrong, the block stops with an AssertionError.

assert 3 < 5
assert not (3 > 5)
assert 4 == 4
assert 4 != 5
assert 5 <= 5
assert not (6 >= 7)
assert type(3 < 5) is bool
age = 20
assert age >= 18 and age < 65
assert (age < 18) or (age == 20)
assert not (age == 21)
assert not (age > 30 or age < 10)
divisor = 0
assert not (divisor != 0 and 10 / divisor > 1)

temperature = 15
assert 10 <= temperature < 20
assert not (20 <= temperature < 30)
assert "yes" == "yes"
assert "Yes" != "yes"

The first check in the last block works only because and stops early. When divisor is 0, the left side is False, so 10 / divisor is never evaluated and no ZeroDivisionError appears.

Common mistakes

  1. Using a bare number as a second condition. The following check passes for any value of score:

    score = 7
    assert score == 2 or 3
    

    Python reads or 3 as a second operand. Any non-zero number counts as True, so the expression is always True. Write assert score == 2 or score == 3 instead.

  2. Comparing a string with a number using <. Writing print("10" < 9) gives:

    TypeError: '<' not supported between instances of 'str' and 'int'
    

    Only values of compatible types can be ordered with <. Convert the text with int("10") first.

  3. Leaving out the left side of a second comparison. Writing print(age >= 18 and <= 65) gives:

    SyntaxError: invalid syntax
    

    Each side of and needs a full comparison, so write age >= 18 and age <= 65.

  4. Expecting == between a string and a number to be True. The check assert "5" == 5 fails:

    AssertionError
    

    The text "5" and the integer 5 are different types, so == gives False. This is the same mistake you saw in the variables lesson.

Practice

  1. Write an assert that checks 12 is greater than or equal to 12, and another that checks 12 is not less than 10.
  2. Store 25 in a variable named x. Write an assert that checks x is greater than 20 and less than 30.
  3. Given temperature = -3, write one assert that uses or to check the temperature is below 0 or above 35.
  4. Given a = 4 and b = 9, write one assert that checks exactly one of a > 5 and b > 5 is True. Hint: compare the two booleans with !=.
  5. Stretch: write an assert that checks 0 < x <= 100 is True when x is 100. Then write an assert that checks the same chained comparison is False when x is 101.
Solution
  1. python assert 12 >= 12 assert not (12 < 10)

  2. python x = 25 assert x > 20 and x < 30

  3. python temperature = -3 assert temperature < 0 or temperature > 35

  4. python a = 4 b = 9 assert (a > 5) != (b > 5)

The first comparison is False and the second is True. They differ, so != gives True.

  1. python x = 100 assert 0 < x <= 100 x = 101 assert not (0 < x <= 100)

Recap

  • A comparison gives True or False. The operators are ==, !=, <, <=, >, and >=.
  • and needs both sides to be True, or needs one, and not flips a boolean. Both and and or stop early when the result is known.
  • Ordering with < between a string and a number raises a TypeError, while == between them gives False.

Sources