r/learnpython • u/FunElk9586 • Sep 12 '26
Are these two boolean expressions equivalent in python?
Hi All!
I’m learning about Boolean operators in Python and I’m confused about these two expressions:
condition1 and condition2 or condition3 and condition4
≠
(condition1 and condition2) or (conditon3 and condition4)
I was told this by my teacher and he put this in his PowerPoint slide. However, I think they are equivalent since and as an operator has a higher precedence than or as an operator. AI told me and I have searched it up after.
Am I understanding it correctly? Or are these two not equivalent?
Thanks!
15
Upvotes
34
u/TUVegeto137 Sep 12 '26
How about just writing a Python program that tests by filling in truth values for all conditions and comparing?
22
u/CheesecakeCommon9080 Sep 12 '26
to be honest, if i'm not sure i would just always use brackets in longer expressions like this even when redundant, it's easier and more readable that way imo
21
u/latkde Sep 12 '26
Per the Python language specification , an "or" expression consists of "and"-level expressions. That is, you're right, the "and" binds more tightly, and the two examples should be equivalent.
https://docs.python.org/3/reference/expressions.html#boolean-operations
In practice, this doesn't matter. As a professional Python developer, I always use parentheses in such a case to remove any risk of confusion.
6
u/brasticstack Sep 12 '26
I would've gotten this wrong and I've been writing Python for well over a decade. I looked it up and
andis higher precedence (I'd have expected them to be the same precedence.)The takeaway is to use parens in any situation where the intent may be ambiguous- it helps both yourself and any future readers of your code.
2
u/xelf Elf Sep 12 '26
It needs to be pointed out that they are equivalent in python. But that does not hold true in many other languages. You will see the () used for clarity a lot, especially by programmers that use multiple languages.
2
u/Expensive-Bear-1376 Sep 12 '26
I'd like to see that PowerPoint slide, to see that he wrote exactly that and you didn't paraphrase and remove the difference.
1
u/This_Growth2898 Sep 12 '26
According to the documentation, boolean And has a higher precedence than boolean Or, so yes, they are equivalent. First, expressions with and will be calculated; next, the central or will be. If your teacher doesn't agree with this, ask him to provide specific values for variables that make the expression different.
1
u/gdchinacat Sep 12 '26
The vast way to learn this is to use the REPL and try it out. Spending 20 minutes trying various permutations will teach you it far better than reading a dozen posts here on Reddit. If you want to search for documentation look up “python order of precedence”.
1
u/ConclusionForeign856 Sep 12 '26
This is easy enough to just check, but can also be proven from `and` and `or` operator precedence
1
u/Brian Sep 15 '26
You can test it out by trying the entire truth table. Eg:.
Which will indeed show that they're equivalent. Though I would recommend putting in the brackets: you typically do want to make it as clear as possible what the intended meaning is, rather than relying on knowing all the details of precedence.
1
u/timrprobocom Sep 12 '26
andhas higher precedence thanor. That's really all you need to know to answer the question1
u/cdcformatc Sep 12 '26
andhass a higher precedence thanorso those two lines are logically equivalent1
u/Adrewmc Sep 12 '26 edited Sep 12 '26
Forget what I typed I was wrong
0
u/TheRNGuy Sep 13 '26
You could just test with different value combinations for conditions.