The math rule you learned in school applies directly to every line of code you write.
The Rule
Order of operations is the set of rules that determines which part of an expression gets calculated first. In math class it was called PEMDAS or BODMAS depending on where you went to school. In code, it is called operator precedence, and every language has its own version of the same idea.
The math order:
- Parentheses
- Exponents
- Multiplication and Division (left to right)
- Addition and Subtraction (left to right)
Why It Matters in Code
What does this produce?
result = 2 + 3 * 4
If you read it left to right you might expect 20. The actual answer is 14, because multiplication runs before addition.
result = 2 + 3 * 4 # 14
result = (2 + 3) * 4 # 20, parentheses force the addition first
This is not a Python quirk. It works the same way in JavaScript, C#, Java, and most other languages.
Operator Priority in Python
Higher in the list runs first.
| Priority | Operators | Example |
|---|---|---|
| 1 (highest) | Parentheses () |
(2 + 3) |
| 2 | Exponent ** |
2 ** 3 equals 8 |
| 3 | Multiplication, Division, Modulo * / % |
10 % 3 equals 1 |
| 4 | Addition, Subtraction + - |
5 - 2 |
| 5 | Comparison == != < > <= >= |
x > 5 |
| 6 | Logical not and or |
x > 5 and y < 10 |
The full list is in the Python operator precedence docs.
Modulo (%) shares a precedence level with multiplication and division, but it has enough quirks of its own that it deserves a closer look. A dedicated post on modulo is coming soon.
A Bug That Hides in Plain Sight
and has higher precedence than or. That one trips people up more than any other.
age = 70
is_member = False
spent = 30
if age > 65 or is_member and spent > 50:
print("Discount applied")
Because and runs before or, Python reads this as:
if age > 65 or (is_member and spent > 50):
So a 70-year-old who spent nothing and is not a member still gets the discount. That may or may not be what you wanted. The fix is to write exactly what you mean with parentheses:
if (age > 65 or is_member) and spent > 50:
print("Discount applied")
Now the user must have spent over $50 AND meet one of the other conditions.
In Excel
Excel uses the same order:
- Parentheses
- Exponents
^ - Multiplication and Division
* / - Addition and Subtraction
+ -
A common mistake with averages:
=A1+A2/2 # divides A2 by 2, then adds A1. Wrong.
=(A1+A2)/2 # adds first, then divides. Correct.
See Excel operator precedence for the full reference.
The Rule That Solves This Every Time
When you are not sure which part runs first, add parentheses. They cost nothing and make your intent clear to anyone reading the code later, including yourself.
# Hard to read at a glance
result = a + b * c - d / e ** 2
# Clear
result = a + (b * c) - (d / (e ** 2))
Both produce the same answer. The second one you can read without thinking about it.
References
- Python operator precedence
- MDN: JavaScript operator precedence
- Excel calculation operators and precedence
- Math for Programmers by Paul Orland (Manning, 2021) - Chapter 1 covers expressions and operator notation with real code examples
Something behaved differently than you expected in your own code? Post the expression below and we will work through it.
Ready to go further? Order of Operations: The Deep Dive
