Python · basics

Logical Operations

Combine conditions with and, or and not, and learn when Python stops early.

The and operator


          # and - both must be True
          True and True
          # ans: True
          True and False
          # ans: False
          False and True
          # ans: False
          False and False
          # ans: False
        

Two switches in a row: the lamp lights only if both are closed.

The or operator


          # or - at least one True
          True or False
          # ans: True
          False or True
          # ans: True
          False or False
          # ans: False
          True or True
          # ans: True
        

Two switches side by side: either one closed lights the lamp.

The not operator


          # not - inverts boolean
          not True
          # ans: False
          not False
          # ans: True
          not (5 > 3)
          # ans: False
        
  1. not (5 > 3)
  2. not True
  3. False

Combining conditions


          # combining with and
          x = 10
          x > 5 and x < 15
          # ans: True
          # combining with or
          x < 5 or x > 8
          # ans: True
        
  1. 10 > 5 and 10 < 15
  2. True and True
  3. True
  1. 10 < 5 or 10 > 8
  2. False or True
  3. True

Short-circuit evaluation


          # and stops at first False
          False and (1/0)
          # ans: False (no error, doesn't evaluate 1/0)
          # or stops at first True
          True or (1/0)
          # ans: True (no error)
        
  1. False and (1/0)already decided
  2. False
  1. True or (1/0)already decided
  2. True

Once the left side settles the answer, Python never runs the right side, so the division by zero never happens.

Exercises

Work out each value, then press →.

Exercises · part 1


        # what is the result?
        True and True
        # ans: True
        # short circuit evaluation?
        False and (1/0)
        # ans: False (no error, doesn't evaluate second part)
      

Exercises · part 2


        # short circuit with or?
        True or (1/0)
        # ans: True (no error)
        # what about this?
        not False
        # ans: True
      

Exercises · part 3


          # chaining and/or?
          True or False and False
          # ans: True (and has higher precedence)
          # boolean with comparison?
          5 > 3 and 10 < 20
          # ans: True
        
  1. True or (False and False)
  2. True or False
  3. True
  1. 5 > 3 and 10 < 20
  2. True and True
  3. True

Exercises · part 4


          # tricky: what is this?
          bool([]) and bool([1])
          # ans: False
          # what evaluates?
          0 or 5
          # ans: 5 (returns last truthy value)
        
  1. bool([]) and bool([1])
  2. False
  1. 0 or 50 is falsy, keep going
  2. 5

and / or return one of their operands, not necessarily True or False.

Exercises · part 5


          # and returns?
          5 and 10
          # ans: 10 (returns last value if all truthy)
          # mixed types?
          "hello" and ""
          # ans: "" (returns last value or first falsy)
        
  1. 5 and 105 is truthy, keep going
  2. 10
  1. "hello" and ""truthy, keep going
  2. ""

Exercises · part 6


          # what about this?
          not not True
          # ans: True
          # De Morgan's law?
          not (True and False)
          # ans: True
          not True or not False
          # ans: True
        
  1. not not True
  2. not False
  3. True
  1. not (True and False)
  2. not False
  3. True
  1. not True or not False
  2. False or True
  3. True

De Morgan: not (a and b) is the same as not a or not b.

Exercises · part 7


          # order of precedence?
          True and False or True
          # ans: True (and first, then or)
          # empty values?
          [] or [1, 2]
          # ans: [1, 2]
        
  1. (True and False) or True
  2. False or True
  3. True
  1. [] or [1, 2]empty list is falsy
  2. [1, 2]

Remember

and

True only if both sides are true.

or

True if at least one side is true.

Order

not binds tightest, then and, then or.

Short circuit

Python stops as soon as the answer is known and returns the value that decided it.