[Tutor] Python Question

Peter Otten __peter__ at web.de
Tue Jul 6 02:33:45 EDT 2021


On 05/07/2021 10:11, Alan Gauld via Tutor wrote:
> On 05/07/2021 00:55, riskxexbiz--- via Tutor wrote:

> Picking out one line:
> 
>> if investor != ["conservative" or "moderate" or "aggressive"]:
> 
> This does not do what you want. Python evaluates the bit
> inside the list as [(True or True or True)] so your code
> is read by Python as:
> 
> if investor != [True]

Let's see:

 >>> ["conservative" or "moderate" or "aggressive"]
['conservative']

So no, the expression

a or b or c

picks the first value that evaluates to True in a boolean context, i. e. 
with bool(value) == True. If there is no such -- sometimes called 
"truthy" -- value the expression evaluates to the last tested value, e. g.:

 >>> 0 or "" or 0.0
0.0
 >>> 0 or 0.0 or ""
''




More information about the Tutor mailing list