Python etc / eval order

Published: 8 April 2021, 18:00

Python uses eager evaluation. When a function is called, all its arguments are evaluated from left to right and only then their results are passed into the function:

print(print(1) or 2, print(3) or 4)
# 1
# 3
# 2 4

Operators and and or are lazy, the right value is evaluated only if needed (for or if the left value is falsy, and for and if the left value is truthy):

print(1) or print(2) and print(3)
# 1
# 2

For mathematical operators, the precedence is how it is in math:

1 + 2 * 3
# 7

The most interesting case is operator ** (power) which is (supposedly, the only thing in Python which is) evaluated from right to left:

2 ** 3 ** 4 == 2 ** (3 ** 4)
# True