[Edu-sig] Truth values and comparisons

Arthur ajsiegel at optonline.net
Mon Oct 30 17:49:02 CET 2006

davelist at mac.com wrote:

>I've not used .any or .all, but having just taught my CS1 students about boolean operators, I was reminded that Python works as the following example describes:
>x = a and b
># if both a and b are true, x is assigned b, otherwise x is assigned a
>x = 2 and 3 # x is assigned 3
>x = 0 and 2 # x is assigned 0
>x = a or b
># if a is true, x is assigned a, if a is true, x is assigned a, if a is false and b is true, x is assigned b, if both a and b are false, x is assigned False
>You need to determine what should make vertex_map[tp] be true...
thanks, but having some trouble:

 >>> import Numeric as N
 >>> a=N.array([0,0,0])
 >>> b=N.array([0,0,1])
 >>> a and b
array([0, 0, 0])
 >>> b and  a
array([0, 0, 0])

Can this be?  Either both a and b are true, or they are not,  so can it 
be returning the "a" in both cases? Something screwy, other than my 
comprehension here?

Same problem with

 >>> a or b
array([0, 0, 1])
 >>> b or a
array([0, 0, 1])

OTOH, this makes sense to me - with "0" being False

 >>> any(a)
 >>> all(a)
 >>> any(b)
 >>> all(b)

Though anyone growing up with the Python boolean operator might wonder 
why it is as it is - i.e. when 0 was the way to spell False this 
behavior is fairly well expected.  Now that False is spelled "False", 
having "0" any less true than "1", when thinks one is dealing with 
numbers as numbers, is likely to catch the less geeky unaware, IMO.


More information about the Edu-sig mailing list