Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder2.enfer-du-nord.net!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: Duncan Booth Newsgroups: comp.lang.python Subject: Re: Equivalent code to the bool() built-in function Date: 28 Apr 2011 17:18:36 GMT Lines: 28 Message-ID: References: <4da9fb0b$0$13696$426a74cc@news.free.fr> Reply-To: duncan.booth@suttoncourtenay.org.uk Mime-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: 8bit X-Trace: individual.net kmBc3wvgmPUz5JN/ZkiNLwrCx6ja022E+kEHp+smuFhneZ+iOf Cancel-Lock: sha1:cTUomd6hmndwapguF6nyvErLoXg= User-Agent: Xnews/2006.08.24 Hamster/2.1.0.11 X-Face: .C;/v3#w@2k.C(.1v-}d=`|7AQ-%,#A$0ZGtAkLPvuawAM>3#D,pXaAb31%(=Gn2ZZK/Z~fd0y4't5iKK~F":}F2*|\mQYX+BUr4ZM*|+`@o-TKzFGwsJnan{)*b~QJ-Fu^u'$$nYV Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:4245 Albert van der Horst wrote: >>I guess I never thought about it, but there isn't an 'xor' operator to >>go along with 'or' and 'and'. Must not be something I need very often. > > There is. <> applied to booleans is xor. > Best to get into the habit of using '!=' otherwise you'll find Python 3.x a bit frustrating. However, that only works if you have exactly two values: both 'and' and 'or' will extend easily to any number of values: >>> True and False and True False >>> False or False or True or False True but using != for 'xor' doesn't extend the same way: >>> False != False != True False You can use 'sum(...)==1' for a larger number of values but do have to be careful that all of them are bools (or 0|1). -- Duncan Booth http://kupuguy.blogspot.com