r/learnpython • • 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!

14 Upvotes

49 comments sorted by

View all comments

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/FunElk9586 Sep 13 '26

That’s good to know. I didn’t know it differs. That makes using parentheses for clarity make even more sense.

1

u/le_sasch Sep 15 '26

In my experience all common languages implement the same precedence (not - and - or) which are also common when writing down boolean algebra in electronics or philosophy.

1

u/xelf Elf Sep 15 '26

Actually there's not a lot when you start looking. Languages that evaluate left to right like APL or bash, or ones that use prefix notation like lisp and clojure. When I actually started looking into it there was less than I remembered.