01 / 20
+ − * /

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.

Python

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

+ Addition
\( 5 + 3 = 8 \)
- Subtraction
\( 10 - 4 = 6 \)
* Multiplication
\( 7 \times 6 = 42 \)
/ Division
\( 15 / 4 = 3.75 \)
// Floor Division
\( 15 // 4 = 3 \)
% Modulus
\( 15 \% 4 = 3 \)
** Exponent
\( 2^{5} = 32 \)

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.

# Converting Fahrenheit to Celsius fahrenheit = 98.6 celsius = (fahrenheit - 32) * 5 / 9 print(celsius) # 37.0

/ vs // vs %

/ Division 17 / 5 3.4 Exact decimal Always returns float // Floor Div 17 // 5 3 Rounds down Drops the decimal % Modulus 17 % 5 2 The remainder 17 = 5 x 3 + 2

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).

print(2 ** 10) # 1024 print(9 ** 0.5) # 3.0 (square root!) print(5 ** -1) # 0.2 (reciprocal)

Operator Precedence Rules

When an expression has multiple operators, Python evaluates them in a strict priority order, from highest to lowest:

1
Parentheses \((\,)\) : always evaluated first
2
Exponentiation ** : right-to-left
3
Unary +x, -x : positive/negative sign
4
Multiplication, Division, Floor Div, Modulus * / // % : left-to-right
5
Addition, Subtraction + - : left-to-right

Visualizing Precedence

( ) Highest ** +x -x * / // % + - Lowest Evaluated later

Associativity: Left vs Right

When two operators have the same precedence, Python uses associativity to decide which one executes first.

Left-to-Right
+ - * / // %

10 - 3 - 2 means
\( (10 - 3) - 2 = 5 \)
Right-to-Left
** (exponentiation)

2 ** 3 ** 2 means
\( 2^{(3^2)} = 2^{9} = 512 \)

How to Evaluate an Expression

1
Scan for parentheses. Evaluate the innermost ( ) groups first, working outward.
2
Evaluate exponents. Process any ** operators, right-to-left.
3
Apply unary signs. Handle -x or +x prefixes.
4
Multiply/Divide group. Process * / // % left-to-right.
5
Add/Subtract group. Process + - left-to-right to get the final result.

Example 1: Step by Step

result = 2 + 3 * 4 print(result)
1
No parentheses or exponents.
2
Multiplication first: \( 3 \times 4 = \) 12
3
Then addition: \( 2 + \) 12 \( = \) 14

Example 2: Parentheses Change Everything

result = (2 + 3) * 4 print(result)
1
Parentheses first: \( (2 + 3) = \) 5
2
Then multiplication: 5 \( \times 4 = \) 20

Example 3: A Complex Expression

x = 10 + 2 ** 3 * 4 - 6 // 2
1
Exponent: \( 2^{3} = \) 8   →   10 + 8 * 4 - 6 // 2
2
Multiply/Floor-div (L→R): \( 8 \times 4 = \) 32,   \( 6 // 2 = \) 3   →   10 + 32 - 3
3
Add/Sub (L→R): \( 10 + 32 = 42 \),   \( 42 - 3 = \) 39

Tricky Case: Negative Bases

Unary negation (-) has lower precedence than exponentiation (**). This creates a common surprise:

print(-3 ** 2) # -9 (not 9!) print((-3) ** 2) # 9
-3 ** 2
Python reads this as -(3**2) = -(9) = -9
(-3) ** 2
Parentheses force negation first: (-3) squared = 9

Common Mistakes

✕ Using ^ for powers
In Python, ^ is bitwise XOR, not exponentiation. Use ** instead.
✕ Forgetting precedence
Writing total + tax * rate when you meant (total + tax) * rate. Add parentheses!
✕ Integer vs float division
Using / 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?

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?

Question 5 of 5

What does -4 ** 2 produce in Python?