Arithmetic Operators
& Precedence
Master every arithmetic operator in Python and learn the order of operations that determines how expressions like \( 2 + 3 \times 4 \) are evaluated.
What Are Arithmetic Operators?
Arithmetic operators are symbols that tell Python to perform mathematical calculations on numbers. They take one or more values (called operands) and produce a result.
Python provides seven arithmetic operators. Understanding each one, and the order in which Python evaluates them, is essential for writing correct expressions.
The Seven Arithmetic Operators
Why Operators Matter
Nearly every Python program uses arithmetic. Whether you are computing a game score, calculating tax on a purchase, or converting temperature units, operators are the foundation.
/ vs // vs %
The ** Exponentiation Operator
In Python, ** raises a number to a power. It replaces the caret ^ you may know from math class (in Python, ^ means something else entirely).
Operator Precedence Rules
When an expression has multiple operators, Python evaluates them in a strict priority order, from highest to lowest:
** : right-to-left+x, -x : positive/negative sign* / // % : left-to-right+ - : left-to-rightVisualizing Precedence
Associativity: Left vs Right
When two operators have the same precedence, Python uses associativity to decide which one executes first.
+ - * / // %10 - 3 - 2 means\( (10 - 3) - 2 = 5 \)
** (exponentiation)2 ** 3 ** 2 means\( 2^{(3^2)} = 2^{9} = 512 \)
How to Evaluate an Expression
( ) groups first, working outward.** operators, right-to-left.-x or +x prefixes.* / // % left-to-right.+ - left-to-right to get the final result.Example 1: Step by Step
Example 2: Parentheses Change Everything
Example 3: A Complex Expression
10 + 8 * 4 - 6 // 210 + 32 - 3Tricky Case: Negative Bases
Unary negation (-) has lower precedence than exponentiation (**). This creates a common surprise:
Common Mistakes
^ is bitwise XOR, not exponentiation. Use ** instead.total + tax * rate when you meant (total + tax) * rate. Add parentheses!/ when you need an integer result. 7 / 2 gives 3.5, use 7 // 2 for 3.Operator Summary Table
| Operator | Name | Example | Result | Precedence |
|---|---|---|---|---|
** |
Exponent | 2 ** 4 | 16 | Highest |
* |
Multiply | 3 * 5 | 15 | Medium |
/ |
Divide | 7 / 2 | 3.5 | Medium |
// |
Floor Div | 7 // 2 | 3 | Medium |
% |
Modulus | 7 % 2 | 1 | Medium |
+ |
Add | 3 + 4 | 7 | Lowest |
- |
Subtract | 9 - 5 | 4 | Lowest |
Question 1 of 5
What does \( 3 + 5 \times 2 \) evaluate to in Python?
Question 2 of 5
What is the result of 17 % 5?
% operator returns the remainder of integer division. \( 17 \div 5 = 3 \) remainder 2, so 17 % 5 = 2.Question 3 of 5
What does 2 ** 3 ** 2 evaluate to?
Question 4 of 5
Which expression correctly computes the average of three variables a, b, and c?
a + b + c / 3 divides only c by 3. You need parentheses around the sum: (a + b + c) / 3.Question 5 of 5
What does -4 ** 2 produce in Python?
**) has higher precedence than unary negation. Python computes \( 4^2 = 16 \) first, then applies the negative: \( -16 \). Use (-4) ** 2 to get 16.