What is the order of operations in Python?

What is the order of operations in Python?

In Python, operations has a specific order. This will affect how a program runs and what expressions are processed first.

Python's order of operations is the same as that of normal mathematics:

  • Parentheses first ()
  • Then exponentiation **
  • Then multiplication * and division /
  • And then addition + and subtraction -

This is why the following operation:

x = 7
y = 5 
z = 6

print(x + y * z) 

## Output: 37

Would have a different output than this one:

x = 7
y = 5 
z = 6

print((x + y) * z) 

## Output: 72