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

Steven D'Aprano steve+comp.lang.python at pearwood.info
Fri Jun 14 12:31:09 EDT 2013


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.")




-- 
Steven



More information about the Python-list mailing list