[Tutor] The Boolean operator "and"

Jeff Shannon jeff at ccvcorp.com
Fri Aug 6 20:39:59 CEST 2004


Dick Moores wrote:
> I have a question about one sentence in the fine Python book I'm 
> presently studying, Practical Python, by Magnus Lie Hetland (Apress). 
> The sentence is "Actually, if x is false, it returns x--otherwise it 
> returns y." This sentence is in the paragraph on p. 109 that begins,

The key here is that there's a difference between being false, and 
being False. [], {}, 0.0, and 0 are all 'false' as well, and if x is 
one of those things, you'll get that rather than the boolean object False.

 >>> x = []
 >>> y = [1]
 >>>
 >>> x and y
[]
 >>> x or y
[1]
 >>>

If you're using this in a boolean context (i.e. an if statement), then 
the effect is the same as if it were False, but in other contexts the 
distinction can be important.

Jeff Shannon
Technician/Programmer
Credit International



More information about the Tutor mailing list