------------------------------------------------------------------------------
MC logo
Comparison and Boolean Ops
[^] Code Examples
------------------------------------------------------------------------------
<<For Loop II bool.py Break and Continue>>
#!/usr/bin/python3

# Script to demonstrate Python comparison and boolean operators.
import random

# Some relationals.  Relationals in Python can be chained, and are
# interpreted with an implicit and.
c = -2
for a in range(1,4):
    c = c + 4
    for b in range(1,4):
        print('(' + str(a), '<', str(b) + ') ==', a < b, '  ', 
              '(' + str(a), '>=', b, '>', str(c) + ') ==', a >= b > c, '  ',
              '(' + str(a), '==', b, '==', str(c) + ') ==', 
              a == b == c, '  ',
              '(' + str(a), '!=', b, '!=', str(c) + ') ==', a != b != c)
        c = c - 1
print()

# Some boolean operations on comparisons.  You have to spell these out
# Pascal- or Ada- style.  None of this && and || stuff.  (Appeals to
# shiftless typists.)
c = -1
for a in range(0,3):
    c = c + 5
    for b in range(0,3):
        print('(' + str(a), '==', b, 'or', a, '==', c, 'and', b, '<',
              str(c) + ') ==', a == b or a == c and b < c, '  ',
              '(not', a, '<', str(b) + ') ==', not a < b, '  ')
        c = c - 2
print()

# When and or or returns true, it returns the second argument.
c = -1
for a in [0, 3, 4]:
    c = c + 2
    for b in [-2, 0, 5]:
        print('(' + str(a), 'and', b, 'or', str(c) + ') ==',
              a and b or c, '  ',
              '(' + str(a), 'or', b, 'and', str(c) + ') ==',
              a or b and c)
        c = c - 1
print()

# Don't forget the very useful in operator.  This works on most (all?) of the
# built-in data structures, including strings.
some = [2,4,7]
for a in range(1,5):
    if a in some:
        print(a, 'is', end=' ')
    else:
        print(a, 'is not', end=' ')
    print('in', some)

Python comparisons are unusual in that chaining comparisons is allowed, and works reasonably. Chained comparisons translate to a series of anded terms, repeating the middle. For instance, a < b == c <= d is interpreted as a < b and b == c and c <= d. The only other note is that the middle expression (4 in the above example) is evaluated only once. For a constant, this doesn't matter, but it will make a difference for expressions which have a side-effect. Note that this does not always have the desired effect. For instance, 3 != 2 != 3 is true, which might not be what you wanted.

Here's how the rest of the world does it:

Expression C, C++ Java, Pascal, Others Python
5 > 4 > 3 Groups as (5 > 4) > 3,
which becomes 1 > 3,
which is false.
Illegal Translates to
5 > 4 and 4 > 3,
which is true.

Python does have a boolean type, with constants True and False. In addion, the following things are considered false: zero (all types), empty lists, strings, tuples, or dictionaries (the last two of which we have not yet discussed). Also the object None, which is sort of like Java's nil or perl's undef. Comparisons return boolean.

For the boolean operators, and, or and not, we have to break down and write out the words; none of this && and || stuff. And and or are short-circuit. They return the last argument evaluated, which is the one that decided the question. This doesn't make any difference inside an if or while, but it is useful in some contexts. Some other Unix scripting languages operate this way.
<<For Loop II Break and Continue>>