Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

and becomes or and or becomes and

32 views
Skip to first unread message

Stef Mientki

unread,
May 22, 2011, 3:23:03 PM5/22/11
to pytho...@python.org
hello,

must of us will not use single bits these days,
but at first sight, this looks funny :

>>> a=2
>>> b=6
>>> a and b
6
>>> a & b
2
>>> a or b
2
>>> a | b
6

cheers,
Stef

Thomas 'PointedEars' Lahn

unread,
May 22, 2011, 5:57:11 PM5/22/11
to
Stef Mientki wrote:

> must of us will not use single bits these days,
> but at first sight, this looks funny :
>
>>>> a=2
>>>> b=6
>>>> a and b
> 6
>>>> a & b
> 2
>>>> a or b
> 2
>>>> a | b
> 6

Change the order of the operands and see what happens.

--
PointedEars

Bitte keine Kopien per E-Mail. / Please do not Cc: me.

Terry Reedy

unread,
May 22, 2011, 6:14:15 PM5/22/11
to pytho...@python.org
On 5/22/2011 5:57 PM, Thomas 'PointedEars' Lahn wrote:
> Stef Mientki wrote:
>
>> must of us will not use single bits these days,
>> but at first sight, this looks funny :
>>
>>>>> a=2
>>>>> b=6
>>>>> a and b
>> 6
>>>>> a& b

>> 2
>>>>> a or b
>> 2
>>>>> a | b
>> 6
>
> Change the order of the operands and see what happens.

or change a,b to 1,2


--
Terry Jan Reedy

Tim Roberts

unread,
May 22, 2011, 6:39:33 PM5/22/11
to
Stef Mientki <stef.m...@gmail.com> wrote:
>
>must of us will not use single bits these days,
>but at first sight, this looks funny :
>
>>>> a=2
>>>> b=6
>>>> a and b
>6
>>>> a & b
>2
>>>> a or b
>2
>>>> a | b
>6

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".
--
Tim Roberts, ti...@probo.com
Providenza & Boekelheide, Inc.

Chris Angelico

unread,
May 22, 2011, 7:28:40 PM5/22/11
to pytho...@python.org
On Mon, May 23, 2011 at 8:39 AM, Tim Roberts <ti...@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

Steven D'Aprano

unread,
May 22, 2011, 8:30:37 PM5/22/11
to
On Sun, 22 May 2011 15:39:33 -0700, Tim Roberts wrote:

> Stef Mientki <stef.m...@gmail.com> wrote:
>>
>>must of us will not use single bits these days, but at first sight, this
>>looks funny :
>>
>>>>> a=2
>>>>> b=6
>>>>> a and b
>>6
>>>>> a & b
>>2
>>>>> a or b
>>2
>>>>> a | b
>>6
>
> 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".

One of my favourite variations on this is by Abbott and Costello, where
Costello proves that 13*7 = 28 in three different ways.

http://www.youtube.com/watch?v=rLprXHbn19I

--
Steven

Colin J. Williams

unread,
May 23, 2011, 8:20:16 AM5/23/11
to pytho...@python.org
On 22-May-11 15:23 PM, Stef Mientki wrote:
> hello,

>
> must of us will not use single bits these days,
> but at first sight, this looks funny :
>
>>>> a=2
>>>> b=6
>>>> a and b
> 6
>>>> a& b

> 2
>>>> a or b
> 2
>>>> a | b
> 6
>
> cheers,
> Stef
5.2. Boolean Operations — and, or, not

These are the Boolean operations, ordered by ascending priority:
Operation Result Notes
x or y if x is false, then y, else x (1)
x and y if x is false, then x, else y (2)
not x if x is false, then True, else False (3)

The second line is puzzling at first look, but consistent.

It is analogous to the Conditional Expression.
See:
http://docs.python.org/reference/expressions.html#conditional-expressions

Colin W.

rusi

unread,
May 23, 2011, 11:30:25 AM5/23/11
to
On May 23, 5:30 am, Steven D'Aprano <steve

Ha Ha! [You're hired Steven]

bch

unread,
May 28, 2011, 8:27:52 AM5/28/11
to

And of course, a programmer cannot tell the difference between
Halloween and Christmas day.

Chris Angelico

unread,
May 28, 2011, 8:50:40 AM5/28/11
to pytho...@python.org
On Sat, May 28, 2011 at 10:27 PM, bch <bch.itb...@gmail.com> wrote:
> And of course, a programmer cannot tell the difference between
> Halloween and Christmas day.

Well known, of course. But a lot of modern programmers don't speak
octal, they only use another power-of-two base; it's as though
someone's cast a hex on them.

Chris Angelico

Nobody

unread,
May 28, 2011, 9:31:52 AM5/28/11
to
On Sun, 22 May 2011 15:39:33 -0700, Tim Roberts 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".

Not Python, but:

#define SIX 1 + 5
#define NINE 8 + 1
...
printf("six times nine is: %d\n", SIX * NINE);

Chris Angelico

unread,
May 28, 2011, 10:04:03 AM5/28/11
to pytho...@python.org
On Sat, May 28, 2011 at 11:31 PM, Nobody <nob...@nowhere.com> wrote:
> Not Python, but:
>
>        #define SIX  1 + 5
>        #define NINE 8 + 1
>        ...
>        printf("six times nine is: %d\n", SIX * NINE);

*AWESOME*!! That is brilliant!

DNA FTW.

ChrisA

0 new messages