[Edu-sig] Truth values and comparisons

Dan Crosta dcrosta at sccs.swarthmore.edu
Mon Oct 30 19:51:16 CET 2006


Arthur wrote:
> if sum(x) !=0:
>    use this array
> else:
>    use other array.

be careful:

 >>> a = [1, 0, -1]
 >>> if a: print "true"
true
 >>> if sum(a) != 0: print "true"


that's why it's probably safer and more readable to use any() and all(), which i 
believe were both introduced to __builtin__ in py2.5.

- d


More information about the Edu-sig mailing list