[Tutor] or synxtax in if statement

Smith, Jeff jsmith at medplus.com
Fri Aug 31 22:20:05 CEST 2007

That definitely won't work.  How could the language possibly determine
if you meant 

a == b | a == c

as opposed to the literal

a == b | c

What this becomes is

a == (b | c)

Also be aware that | is a "bitwise or" and not a logical "or" which may
not be what you want.  So your original expression may not be what you
want since it will get evaluated as

a == (b | a) == c

Consider the following result:

>>> a=0
>>> b=0
>>> c=1
>>> print a == b | a == c
>>> print a == (b | a) == c
>>> print (a == b) | (a == c)
>>> print a == b | c

Although what I suspect you really want is

>>> print a == b or a == c
>>> print (a == b) or (a == c)

But this means that your shortcut becomes

(a == b) or c

So consider

>>> a=0
>>> b=1
>>> c=0
>>> print a == b or c

Which is the same as false.


-----Original Message-----
From: tutor-bounces at python.org [mailto:tutor-bounces at python.org] On
Behalf Of David Bear
Sent: Friday, August 31, 2007 3:40 PM
To: tutor at python.org
Subject: [Tutor] or synxtax in if statement

I think I want to be lazy and express this

if a == b | a = c
(if a equal b or a equals c)

if a == b | c

it seems to work.. but I'm not sure if it is correct -- and I haven't
seen any documentation on using this type of syntax.

David Bear
College of Public Programs at Arizona State University

Tutor maillist  -  Tutor at python.org

More information about the Tutor mailing list