Python · basics

Comparison Operations

Ask Python a question about two values and get back True or False.

Six questions

OperatorAsksExample
==equal to?5 == 5 → True
!=not equal to?5 != 3 → True
> <greater / less than?3 > 5 → False
>= <=greater / less or equal?5 >= 5 → True

A single = assigns; a double == compares.

Equality


        # equal to
        5 == 5
        # ans: True
        5 == 3
        # ans: False
        "hello" == "hello"
        # ans: True
      

Inequality


        # not equal to
        5 != 3
        # ans: True
        5 != 5
        # ans: False
        "a" != "b"
        # ans: True
      

Greater than / less than


          # greater than
          5 > 3
          # ans: True
          3 > 5
          # ans: False
          # less than
          5 < 3
          # ans: False
          3 < 5
          # ans: True
        

On a number line, "greater" means "further to the right".

Greater / less or equal


        # greater or equal
        5 >= 5
        # ans: True
        5 >= 3
        # ans: True
        # less or equal
        5 <= 3
        # ans: False
        5 <= 5
        # ans: True
      

Chained comparisons


          # chain comparisons
          3 < 5 < 7
          # ans: True (both conditions)
          1 < 2 < 3 < 4
          # ans: True
          5 < 3 < 7
          # ans: False (first fails)
        
  1. 3 < 5 and 5 < 7
  2. True and True
  3. True
  1. 1 < 2 and 2 < 3 and 3 < 4
  2. True
  1. 5 < 3 and 3 < 7
  2. False

A chain is a shorthand for comparisons joined by and; it stops at the first False.

Exercises

True or False? Decide, then press →.

Exercises · part 1


          # comparison chain?
          3 < 5 < 7
          # ans: True
          # what happens?
          5 < 3 < 7
          # ans: False (5 < 3 is False)
        
  1. 3 < 5 and 5 < 7
  2. True and True
  3. True
  1. 5 < 3 and 3 < 7
  2. False

Exercises · part 2


          # comparison with different types?
          5 == 5.0
          # ans: True
          # is vs ==?
          a = [1, 2]
          b = [1, 2]
          a == b
          # ans: True
          a is b
          # ans: False
        

== compares contents; is asks whether both names point at the very same object.

Exercises · part 3


        # what is result?
        "10" == 10
        # ans: False (different types)
        # string comparison?
        "apple" < "banana"
        # ans: True (lexicographic)
      

Strings compare letter by letter, like a dictionary: "a" comes before "b".

Exercises · part 4


        # what about this?
        True == 1
        # ans: True
        False == 0
        # ans: True
      

Exercises · part 5


          # chained equality?
          5 == 5 == 5
          # ans: True
          5 == 5 == 6
          # ans: False
        
  1. 5 == 5 and 5 == 5
  2. True
  1. 5 == 5 and 5 == 6
  2. True and False
  3. False

Exercises · part 6


          # None comparison?
          None == None
          # ans: True
          x = None
          x is None
          # ans: True
        

There is only one None, so x is None is the usual way to test for it.

Exercises · part 7


          # empty comparisons?
          [] == []
          # ans: True
          [] is []
          # ans: False
          "" == ""
          # ans: True
        

Remember

Always a bool

Every comparison gives True or False.

Chains mean and

a < b < c is a < b and b < c.

== vs is

Same contents vs the same object in memory.

Types matter

"10" == 10 is False, but 5 == 5.0 and True == 1 are True.