The Order of Operations in Algebra
The order of operations decides what an expression means: brackets first, then powers, then multiplication and division, then addition and subtraction. With letters in the expression the rules are the same as in arithmetic, but a few cases (negative signs with powers, implied multiplication and fraction bars) trip up both students and programmers. This lesson covers them and compares the rules with Python's operator precedence.
The rules
| Rank | Operation | Notes |
|---|---|---|
| 1 | Grouping: ( ), [ ], fraction bars, | a fraction bar groups its whole top and whole bottom |
| 2 | Exponents | evaluated right to left: |
| 3 | Multiplication and division | same rank, left to right |
| 4 | Addition and subtraction | same rank, left to right |
The arithmetic version is in Order of Operations. Here are the algebra-specific points.
Evaluating an expression with variables
Substitute first, using brackets around each value, then follow the order:
Without the brackets you might write , which is a different number.
The negative-power trap
A minus sign in front of a power is not part of the base unless brackets say so:
Python follows the same convention, because ** binds tighter than the unary minus:
x = 3
print(-x**2) # -(x**2)
print((-x)**2)
print(-3**2, (-3)**2)
print(2**3**2) # right to left: 2**(3**2) = 2**9-9
9
-9 9
512
Note: Spreadsheets break this rule: in Excel,
=-3^2gives 9, because Excel applies the negation first. Code that translates formulas between spreadsheets and programming languages has to handle this.
Implied multiplication and division
In algebra, means , and usually means . In a program you must write the multiplication sign and the brackets yourself, and a/b*c means , because division and multiplication go left to right.
written: 6/2x with x = 3
intended: 6/(2x) = 6/6 = 1 (most textbooks)
in code: 6/2*x = 3*3 = 9 (left to right)Avoid the ambiguity altogether: write or .
Fraction bars and square roots group
A long fraction bar acts as brackets around its numerator and denominator, and a square root sign covers everything under it:
Translating these into code needs explicit brackets. Here is the quadratic formula's discriminant, done wrong and right:
import math
a, b, c = 1, -3, -10
right = math.sqrt(b**2 - 4*a*c)
wrong = math.sqrt(b**2) - 4*a*c
print("right:", right)
print("wrong:", wrong)
x = 4
print((x + 6) / (x - 1), x + 6 / x - 1)right: 7.0
wrong: 43.0
3.3333333333333335 4.5
Python's precedence table
| Higher → lower | Operators |
|---|---|
| exponent | ** |
| unary | -x, +x |
| multiplicative | *, /, //, % |
| additive | +, - |
| comparison | <, <=, ==, !=, >=, > |
| boolean | not, and, or |
Because comparisons come after arithmetic, 2*x + 1 > 7 means (2*x + 1) > 7, exactly as in algebra. Bitwise operators such as & and ^ sit between these ranks and catch people out: in Python ^ is XOR, not a power, so 2^3 is 1.
In software
Precedence bugs are among the most common translation errors from a formula to code: a missing pair of brackets around a denominator, -x**2 where (-x)**2 was meant, or a / b * c where a / (b * c) was meant. When in doubt, add brackets; they cost nothing and make the intent visible to the next reader.
Exercises
Exercise 1: Evaluate
Evaluate for , .
Answer
19
Solution
Exercise 2: Signs and powers
For , evaluate (a) , (b) , (c) , (d) .
Answer
(a) −16, (b) 16, (c) 16, (d) 64
Solution
x = -4
print(-x**2, (-x)**2, x**2, -x**3)-16 16 16 64
Exercise 3: Fix the code
The formula is . A student wrote m = y2 - y1 / x2 - x1. With and , what does the wrong line give, and what is ?
Answer
The wrong line gives 8.6; .
Solution
Without brackets, the division happens first: . With brackets: .
x1, y1, x2, y2 = 1, 2, 5, 10
print(y2 - y1 / x2 - x1, (y2 - y1) / (x2 - x1))8.6 2.0
Exercise 4: Right to left
What are 2**2**3 and (2**2)**3 in Python?
Answer
256 and 64
Solution
** groups from the right: , while .
Test yourself
What is when ?
- 25
- −25
- 10
- −10
In Python, what does a / b * c mean?
- a / (b * c)
- (a / b) * c
- It is a syntax error
- a * c / b only for integers
Which Python line correctly computes ?
- x + 1 / x - 1
- (x + 1) / x - 1
- (x + 1) / (x - 1)
- x + 1 / (x - 1)