Reputation: 4845
How do I tell Python I want to do the following:
if cond1 and (not cond2 or not cond2)
I want the expressions in the parenthesis to execute first and then feed the result to and.
Upvotes: 1
Views: 6766
Reputation: 2532
It's just a matter of operator precedence as described in the Python docs. As you can see in the table below (from highest to lowest precedence), parenthesized expression are the first ones to be executed.
Operator precedence is a common concept in most programming languages.
Moreover, what @Hooked is referring to is called short circuit evaluation.
Upvotes: 0
Reputation: 386342
Like most languages, python evaluates expressions left-to-right, so you simply need to put them in the order that you want them to be evaluated:
if (not cond2 or not cond3) and cond1
Upvotes: 13