A certainl part of an if() structure never gets executed.

Nick the Gr33k support at superhost.gr
Fri Jun 14 12:56:02 EDT 2013


On 14/6/2013 7:31 μμ, Steven D'Aprano wrote:
> On Fri, 14 Jun 2013 16:07:56 +0300, Nick the Gr33k wrote:
>
>> Thanks for explaining this but i cannot follow its logic at all. My mind
>> is stuck trying to interpret it as an English sentence:
>>
>> if ('Parker' and 'May' and '2001')
>>
>> if ('Parker' or 'May' or '2001')
>>
>> i just don't get it and i feel silly about it.
>
> Python is not English. You just have to accept that, no matter how much
> you wish it worked like English, it does not.
>
> Think of it like this:
>
> "If today is Tuesday AND I finish work early, then I can go to the
> movies."
>
> Unless *both* conditions are true, I cannot go to the movies.
>
>
> "If today is Tuesday AND I finish work early AND I have more than $16
> spare cash to pay for a ticket, then I can go to the movies."
>
> All three conditions must be true, or I cannot go to the movies.
>
>
> If today is Monday, I don't need to check whether I finish work early, or
> whether I have spare cash. It is enough to know that today is not
> Tuesday, so I'm not going to the movies.
>
>
> Python works the same way:
>
> today_is_tuesday = True
> finish_work_early = True
> spare_cash = 11
>
>
> if today_is_tuesday and finish_work_early and spare_cash > 16:
>      print("Going to the movies")
> else:
>      print("No movies today.")
>
It will print the latter since the overall boolean evaluation of the 
expression is False since (spare_cash > 16) = False

That's understandable and works just like an English sentence, but in 
this example it was easy since you assigned the vars values to be either 
True or False.

This is my difficulty.

a = 'abcd'
b = 'efgh'
c = 'ijkl'


 >>> (a or b or c)
'abcd'

This for me, should evaluate to True but instead it has been evaluated 
to the first variable's value, which is a truthy value of course since 
its not an empty string, but shouldn't it return True instead?

Returning True is the same thing as returning a variable's truthy value?


 >>> (a and b and c)
'ijkl'

This in my head should have been evaluated to True also since all 3 
strings hold truthy values

Why on earth this boolean expression evaluates to the value of the last 
variable? This is what can't at all seem to follow.


What i'm trying to say that both these exprs are Boolean Expressions 
therefore should return Boolean values not variable's values, even if 
they are truthy.




-- 
What is now proved was at first only imagined!



More information about the Python-list mailing list