Many times the exact change in state that our program needs to make
depends on a condition. A condition is a Boolean expression; an expression
that is either True
or False
.
Generally conditions are on comparisons among variables using the
comparison operations.
We'll look at the essential definitions of truth, the logic
operations and the comparison operations. This will allow us to build
conditions.
Python represents truth and falsity in a variety of ways.

False. Also 0, the special value None
,
zerolength strings ""
, zerolength lists
[]
, zerolength tuples ()
,
empty mappings {}
are all treated as
False
.

True. Anything else that is not equivalent to
False
.
We try to avoid depending on relatively obscure rules for
determining True
vs. False
. We
prefer to use the two explicit keywords, True
and
False
. Note that a previous version of Python didn't
have the boolean literals, and some older opensource programs will
define these values.
Python provides a factory function to collapse these various forms
of truth into one of the two explicit boolean objects.

bool
(
object
) →
boolean

Returns True
when the argument
object
is one the values equivalent to
truth, False
otherwise.
Python provides three basic logic operators that work on this
Boolean domain. Note that this Boolean domain, with just two values,
True
and False
, and these three
operators form a complete algebraic system, sometimes called Boolean
algebra, after the mathemetician George Boole. The operators supported
by Python are
not
,
and
and
or
. We can fully define these operators with rule
statements or truth tables.
This truth table shows the evaluation of
not
x
for both vales of
x
.
print "x", "not x"
print True, not True
print False, not False
x 
not x 
True 
False 
False 
True 
This table shows the evaluation of
x
and
y
for all combination of
True
and False
.
print "x", "y", "x and y"
print True, True, True and True
print True, False, True and False
print False, True, False and True
print False, False, False and False
x 
y 
x and y 
True 
True 
True 
True 
False 
False 
False 
True 
False 
False 
False 
False 
An important feature of
and
is that it does not
evaluate all of its parameters before it is applied. If the lefthand
side is False
or one of the equivalent values, the
righthand side is not evaluated, and the lefthand value is returned.
We'll look at some examples of this later.
For now, you can try things like the following.
print False and 23
print 23 and False
This will show you that the first false value is what Python
returns for
and
.
This table shows the evaluation of
x
or
y
for all combination of
True
and False
.
x 
y 
x or y 
True 
True 
True 
True 
False 
True 
False 
True 
True 
False 
False 
False 
Parallel with the
and
operator,
or
does not evaluate the righthand parameter if the
lefthand side is True
or one of the equivalent
values.
As a final note,
and
is a high priority
operator (analogous to multiplication) and
or
is
lower priority (analogous to addition). When evaluating expressions like
a or b and c
, the
and
operation is
evaluated first, followed by the
or
operation.