Ken Masters
Ken Masters

Reputation: 281

Python if conditionals with parentheses

Currently, I encountered an issue regarding data processing. There are 4 lists as below:

a=["-99997","-99999","-99995","-99997"]
b=["212","215","-99999","222"]
c=["-99995","-99999","-99999","-99997"]
d=["422","-99995","565","667"]

The main list is "a".

My purpose is to make a loop of this 4 list. If the items in list "a" are "-99997", "-99999" or "-99995" and the corresponding items in other lists are not "-99997", "-99999", "-99995 then the corresponding items will replace those "-99997", "-99999", "-99995 in list "a".

Therefore, the output should be:

a=["212","215","565","222"]

I did figure out the simplified version of the code as below:

i=0
for value,prismattribute in zip(a,d):
 if (value=="-99999" or value=="-99997" or value=="-99995") and prismattribute!="-99999" and prismattribute!="-99997" and prismattribute!="-99995":
 a[i]=prismattribute
 i=i+1
 print(a)
 else:
 i=i+1

This code has parentheses in the if statement which give me the desired output:

['422', '-99999', '-99995', '-99997']
['422', '-99999', '565', '-99997']
['422', '-99999', '565', '667']

But for the code without parentheses:

i=0
for value,prismattribute in zip(a,d):
 if value=="-99999" or value=="-99997" or value=="-99995" and prismattribute!="-99999" and prismattribute!="-99997" and prismattribute!="-99995":
 a[i]=prismattribute
 i=i+1
 print(a)
 else:
 i=i+1

The result is:

['422', '-99999', '-99995', '-99997']
['422', '-99995', '-99995', '-99997']
['422', '-99995', '565', '-99997']
['422', '-99995', '565', '667']

So, in the second iteration of the loop, the "non-parentheses" code evaluated the condition as True - It replaced the second item of list "a" with the value "-99995" of list "d", while the parentheses code evaluated the condition as False - which skipped to next iteration.

For this complex condition, I am confused about how the if statement with combination of "and", "or" and parentheses () works - as I expected the 2 versions of the code would give me the same result. Can you enlighten me on this matter ?

Upvotes: 0

Views: 3480

Answers (2)

Random Davis
Random Davis

Reputation: 6857

Boolean operators have precedence in Python and in general, just like arithmetic operators: https://docs.python.org/3/reference/expressions.html#operator-precedence

To check if two Boolean expressions are equivalent, you can run them through a parser, like this online one: https://www.dcode.fr/boolean-expressions-calculator

Your first example, with parentheses, is equivalent to the following logical expression:

(a or b or c) and (d and e and f)

Your second example, without parentheses, has these implicit parentheses due to operator precedence:

(a or b) or (c and d and e and f)

This is equivalent to if it was a mathematical expression. If we follow PEMDAS, the following equation:

a + b + c * d * e * f

is equivalent to:

(a + b) + (c * d * e * f)

Whereas if we change the location of the parentheses, it can change the outcome:

(a + b + c) * (d * e * f)

Upvotes: 3

Ogün Birinci
Ogün Birinci

Reputation: 728

For this question, I think you should check the documentation

https://docs.python.org/3/reference/expressions.html#operator-precedence

According to the documentation, and operator has higher precedence according to the or operator. So this is affect the result directly in the or value=="-99995" and prismattribute!="-99999"part

Upvotes: 2

Related Questions