and becomes or and or becomes and

Chris Angelico rosuav at gmail.com
Sun May 22 19:28:40 EDT 2011


On Mon, May 23, 2011 at 8:39 AM, Tim Roberts <timr at probo.com> wrote:
> That IS funny.  Interesting how a careful choice of arugments will fool us.
> One of my favorite math jokes is like that.  A teacher asked a student to
> reduce the following fraction:
>  16
>  ----
>  64
>
> He says "all I have to do is cancel out the sixes, so the answer is 1/4".

I like. :)

But in the OP, the difference between "and" and "&", or "or" and "|",
is subtle yet absolute. They are completely different operators. The
bitwise operators function like the arithmetic operators - evaluate
both operands, then do something that combines them into one value.
The logical operators, though, are more like the if statement:

q = a and b

is similar to:

if a:
  q = a
else:
  q = b

(Pedants, please note that I said "similar" not "equivalent".) They
happen to do similar things, but they're completely different in
operation. I do like the humour value from the careful selection of
operands though!

Chris Angelico



More information about the Python-list mailing list