You can always ask Python to print out how it parses your code using the ast
(abstract syntax tree) module:
$ echo 'a+b==20 or a==20 and b==20' | python3 -m ast
Module(
body=[
Expr(
value=BoolOp(
op=Or(),
values=[
Compare(
left=BinOp(
left=Name(id='a', ctx=Load()),
op=Add(),
right=Name(id='b', ctx=Load())),
ops=[
Eq()],
comparators=[
Constant(value=20)]),
BoolOp(
op=And(),
values=[
Compare(
left=Name(id='a', ctx=Load()),
ops=[
Eq()],
comparators=[
Constant(value=20)]),
Compare(
left=Name(id='b', ctx=Load()),
ops=[
Eq()],
comparators=[
Constant(value=20)])])]))],
type_ignores=[])
The tree is a bit verbose, but you can see the topmost operation is OR
, followed by a nested AND
, i.e. Python parses your expression as
(a+b==20) or (a==20 and b==20)
(And, of course, the precedence order is well documented).