# The AND/OR part Re: [Tutor] Sample9.py and Sample9.pyc & seting path to Python open files ...

Magnus Lycka magnus at thinkware.se
Wed Nov 26 13:28:39 EST 2003

```> So in the one from techiwarehouse.com, there is an explanation of so-called Logical operators: and, or, and not. I do not understand explanation of and operator:

In Boolean arithmetic, there are only two values, True (let's
call that 1 for the moment) and False, let's call that 0 for
now. There are also three operators, AND (we'll call that *),
OR (let's call that +) and finally NOT (let's call that !).

This arithemtic is based on a philosophical concept of logic
and truth. IF the battery works, AND the light bulb is working
AND I turn it on, my lamp will emit light.

Written as an equation:

emit_light = battery_works AND light_bulb_works AND turned_on

Or rephrased:

IF the battery does NOT work OR the light bulb is NOT working
or I do NOT turn it on, the lamp will NOT emit light.

Written as an equation:

NOT emit_light = (NOT battery_works) OR (NOT light_bulb_works) OR
(NOT turned_on)

(Actually, this transformation from AND to NOT/OR is called
de Morgans theorem, but that doesn't matter now.)

In general:

True AND True = True
True AND False = False
False AND True = False
False AND False = False

True AND True = True
True AND False = False
False AND True = False
False AND False = False

NOT True = False
NOT False = True

Written with the shorter notation I suggested above, we get:

1 * 1 = 1
1 * 0 = 0
0 * 1 = 0
0 * 0 = 0

1 + 1 = 1
1 + 0 = 1
0 + 1 = 1
0 + 0 = 0

!1 = 0
!0 = 1

As you see, AND behaves exactly like multiplication,
and OR is very close to addition, with the little
quirk that 1 + 1 = 0. Note that this is not normal
binary arithmetic, where 1 + 1 = 10. Still, it's
close enough to normal arithemtic for 1, 0 and *, +
to become popular symbols for True, False and AND, OR.

If you generalize the equations above, and let X or Y
represent values that can be either True or False,
we can say that

if X is true:
X AND Y = Y
X OR Y = X

if X is false:
X AND Y = X
X OR Y = Y

Are you following this? It's a generalization of

1 AND 1 = 1
1 AND 0 = 0
0 AND 1 = 0
0 AND 0 = 0

1 OR 1 = 1
1 OR 0 = 1
0 OR 1 = 1
0 OR 0 = 0

If Boolean theory, this assumes that X is either true
or false. In some programming languages, such as Python,
this concept has been extended a bit.

Basically, in Python, 0, 0j, 0.0, "", [], (), {}, None
and instances of classes where the __nonzero__ method returns
a false value are false. All (?) other values are true.

So, this...

>>> 0 or 5
5

..is completely in line with George Boole. Five is one of
many representations of True, and 0 is one of several
representations of False, so what is says above is "False
AND True = True" That's completely correct.

A possible problem occurs if you have to completely unfounded
idea that all truths are equal, and that all incarnations of
false are equal, since for instance

>>> (0 or 5) == (0 or 3)
False

One truth isn't always identical another... (Or as another
George put it: "All animals are equal, but some are more
equal than others." ;)

In the case of comparisions (<, <=, ==, !=, > and >=) Python
returns the values True (which is basically 1 on disguise)
and False (which is basically 0 in disguise). In previous
versions it returned 1 and 0. The not-operator also returns
True or False.

>>> not 0
True
>>> 1 == ''
False
>>> not "Hello"
False
>>> 5 < 3
False

It would be very odd if "1 == 1" would return 42 or some
other arbitrary value, but in the case of AND and OR, it's
very useful to use the generalization I mentioned above:

if X is true:
X AND Y = Y
X OR Y = X

if X is false:
X AND Y = X
X OR Y = Y

This enables us to use AND and OR as very compact versions
of if statements that we can put inside expressions. For
instance, if we have a variable X that can either contain
an integer on the value None, and we want to treat None as
0 from a mathermatical perspective, we can write something
like...

R = ((X or 0) + Y) * Z

if X:
R = (X + Y) * Z
else:
R = Y * Z

Or, if you are displaying email messages, you could use
something like...

print subject or "<no subject>"

if subject:
print subject
else:
print "<no subject>"

I think it's less common that the and-operator is used
like this in Python, but for instance, you could do...

y = (x >= 0 and x) * 5

if x > 0:
y = x > 5
else:
y = 0

--
Magnus Lycka, Thinkware AB
Alvans vag 99, SE-907 50 UMEA, SWEDEN
phone: int+46 70 582 80 65, fax: int+46 70 612 80 65
http://www.thinkware.se/  mailto:magnus at thinkware.se

```