SECTION 1
Historical Context & Motivation
Long before computers existed, mathematicians needed a shared set of rules for evaluating expressions. Imagine if two people looked at the expression 3 + 5 × 2 and got different answers — one computing 16, the other 13. Without a universal agreement on the order of operations, mathematical communication would be chaos. These conventions were gradually standardized over centuries so that every person — and eventually every computer — would interpret a formula in exactly the same way.
1600s • Symbolic Algebra Emerges
1900s • PEMDAS is Widely Taught
1950s–60s • Early Programming Languages
1991 • Python Is Created
The central question this lesson addresses is straightforward but critically important: when Python sees a line like 10 + 3 * 2 ** 2, how does it decide which operation to perform first, second, and third? Understanding operator precedence is the key to writing correct calculations in every Python program you will ever build.
SECTION 2
Core Principles & Definitions
Python provides seven arithmetic operators that let you perform math on numbers. Each operator has a specific precedence level — a ranking that tells Python which operations to carry out first when multiple operators appear in the same expression. Operators with higher precedence are evaluated before operators with lower precedence. When two operators share the same precedence, Python uses associativity rules (usually left-to-right) to break the tie.
Operators
+, -, *, /, //, %, and ** that perform arithmetic on values called operands.Precedence
Associativity
**) associates right-to-left.Parentheses Override
( ) to force a specific evaluation order. Whatever is inside parentheses is calculated first, regardless of default precedence.Integer vs. Float Results
+ - * // % ** on integers (except / which always returns a float). Mixing an int with a float produces a float.KEY TAKEAWAY
** (exponentiation) is the 'fire alarm' — it gets handled before multiplication or addition, no matter where it appears in the expression. Parentheses are like your boss physically grabbing you and saying, 'Do THIS right now.'SECTION 3
Visual Explanation — The Precedence Pyramid
Reading the pyramid from top to bottom gives you Python's evaluation order. When you write an expression like 2 + 3 * 4 ** 2, Python first evaluates 4 ** 2 (exponentiation → 16), then 3 * 16 (multiplication → 48), and finally 2 + 48 (addition → 50). The pyramid is your roadmap for predicting how any expression will be computed.
SECTION 4
How Each Operator Works
Let's examine every arithmetic operator Python offers. Understanding what each one does — and what type of result it returns — is essential before tackling precedence in complex expressions.
| Operator | Name | Example | Result | Return Type |
|---|---|---|---|---|
+ | Addition | 7 + 3 | 10 | int (if both int) |
- | Subtraction | 10 - 4 | 6 | int (if both int) |
* | Multiplication | 5 * 6 | 30 | int (if both int) |
/ | True Division | 7 / 2 | 3.5 | Always float |
// | Floor Division | 7 // 2 | 3 | int (if both int) |
% | Modulo (Remainder) | 7 % 2 | 1 | int (if both int) |
** | Exponentiation | 2 ** 3 | 8 | int (if both int) |
Python's seven arithmetic operators
Key Details Worth Noting
True division (/) always returns a float, even when dividing two integers evenly. For example, 6 / 3 gives 2.0, not 2. Floor division (//) rounds the result down to the nearest whole number. The modulo operator (%) returns the remainder after division — it is incredibly useful for checking if a number is even or odd, or for cyclic patterns like days of the week.
Watch Out: Negative Floor Division
-7 // 2 gives -4, not -3. This surprises many beginners. Python always rounds down, meaning it goes to the more negative number.SECTION 5
Detailed Breakdown — Tracing an Expression
The best way to master precedence is to trace through an expression step by step, watching Python decide which operation to perform at each stage. The diagram below shows how Python evaluates the expression 2 + 3 * 4 ** 2 - 8 // 3 by building an evaluation tree.
2 + 3 * 4 ** 2 - 8 // 3. Yellow marks the exponentiation step, cyan marks multiplication and floor division (same precedence, evaluated left-to-right), and violet marks the final addition and subtraction.The PEMDAS Connection
You may already know the PEMDAS rule from math class: Parentheses, Exponents, Multiplication/Division, Addition/Subtraction. Python follows this same order almost exactly. The only additions are // (floor division) and % (modulo), which sit at the same level as * and /. Remember that M and D in PEMDAS are equals (left-to-right), and so are A and S. Python handles them the same way.
Right-to-Left Exponentiation
2 ** 3 ** 2 is evaluated as 2 ** (3 ** 2) = 2 ** 9 = 512, NOT as (2 ** 3) ** 2 = 64. If you're ever unsure, just use parentheses!SECTION 6
Worked Example
Suppose you're writing a Python program to calculate a student's final grade. The formula is: the average of three test scores, plus 5 bonus points, all divided by 2 for a curve. Let's trace how precedence affects this computation.
Calculating a Curved Grade
- 1
Step 1 — Write the expression
Your three test scores are 80, 90, and 70. A first attempt might look like this:result = 80 + 90 + 70 / 3 + 5 / 2But this expression does NOT compute what we want! - 2
Step 2 — Trace using precedence (incorrect version)
Python evaluates division before addition. So it first computes70 / 3≈ 23.33 and5 / 2= 2.5. Then it adds everything left-to-right: 80 + 90 + 23.33 + 2.5 = 195.83. That's nowhere near a grade out of 100!195.83— clearly wrong - 3
Step 3 — Fix with parentheses
We need parentheses to override the default precedence and tell Python exactly what we mean:result = ((80 + 90 + 70) / 3 + 5) / 2Parentheses enforce our intended order of operations. - 4
Step 4 — Trace the corrected expression
Innermost parentheses first:80 + 90 + 70= 240. Then240 / 3= 80.0. Then80.0 + 5= 85.0. Finally,85.0 / 2= 42.5.result = 42.5 - 5
Step 5 — Verify in Python
You can verify this by opening a Python interpreter and typing the expression. Python will confirm:>>> ((80 + 90 + 70) / 3 + 5) / 242.5Always test expressions in the Python shell when you're unsure about precedence. It takes seconds and prevents bugs.✓ Confirmed: 42.5
SECTION 7
Common Pitfalls & Best Practices
Even experienced programmers make mistakes with operator precedence. The table below highlights the most frequent pitfalls and how to avoid them.
| Pitfall | Example | What You Expected | What Python Does |
|---|---|---|---|
| Forgetting division is higher than addition | 100 + 50 / 2 | 75.0 | 125.0 (divides first: 50/2=25, then 100+25) |
| Chained exponentiation direction | 2 ** 3 ** 2 | 64 (left-to-right) | 512 (right-to-left: 3²=9, then 2⁹) |
| True division returns float | 10 / 5 | 2 (integer) | 2.0 (float — use // if you want int) |
| Negative floor division | -7 // 2 | −3 | −4 (rounds toward negative infinity) |
| Mixing int and float | 5 + 2.0 | 7 (integer) | 7.0 (float — Python 'promotes' to float) |
Common precedence and type pitfalls
BEST PRACTICE
(a * b) + c costs nothing and saves everyone — including your future self — from guessing what you meant. Clear code is always better than clever code.SECTION 8
Connection to Advanced Operators & Expressions
Arithmetic precedence is just one layer of Python's larger operator precedence table. As you advance in Python, you'll encounter comparison operators (==, <, >), logical operators (and, or, not), and bitwise operators. All of these fit into the same precedence hierarchy, and arithmetic operators generally sit above comparison and logical operators.
| Concept | What You Know Now | What Comes Next |
|---|---|---|
| Operators | Seven arithmetic operators: + − * / // % ** | Comparison (==, !=, <, >), logical (and, or, not), assignment (+=, *=) |
| Precedence | Parentheses → ** → * / // % → + − | Full 18-level Python precedence table including bitwise, shift, and ternary operators |
| Data Types | int and float results from arithmetic | Complex numbers, Decimal for financial math, operator overloading in custom classes |
| Expressions | Simple arithmetic expressions | Boolean expressions, list comprehensions, lambda functions, f-strings with embedded expressions |
From arithmetic to advanced Python expressions
The great news is that the mental model you've built today — precedence tiers, associativity rules, and parentheses as overrides — applies to every operator Python will ever throw at you. Mastering arithmetic precedence gives you the foundation for understanding all expression evaluation in programming.
SECTION 9
Practice Problems
Question 1: CONCEPTUAL
5 + 3 * 2, why does Python return 11 instead of 16? Explain which rule of precedence is at work.Show answer
3 * 2 is evaluated first, yielding 6. Then 5 + 6 is computed, giving 11. If you wanted 16, you would need to write (5 + 3) * 2 to force the addition to happen first using parentheses.Question 2: BASIC CALCULATION
10 + 6 // 4 * 3 - 1Show answer
// and * share the same level, so evaluate left-to-right. First, 6 // 4 = 1 (floor division). Next, 1 * 3 = 3. Now handle addition and subtraction left-to-right: 10 + 3 = 13, then 13 - 1 = 12. The final answer is 12.Question 3: INTERMEDIATE
3 ** 2 ** 2 + 10 % 3Show answer
2 ** 2 is computed first, giving 4. Then 3 ** 4 = 81. Next, modulo: 10 % 3 = 1 (remainder of 10 ÷ 3). Finally, 81 + 1 = 82. If exponentiation were left-to-right, 3 ** 2 would be 9, then 9 ** 2 = 81 — same result coincidentally, but try 2 ** 3 ** 2 to see the difference (512 vs. 64).Question 4: APPLIED
share = 85 + 85 * 18 / 100 / 4
Is this correct? If not, write a corrected version using parentheses and compute the result.Show answer
85 * 18 = 1530, then 1530 / 100 = 15.3, then 15.3 / 4 = 3.825, and finally 85 + 3.825 = 88.825. This splits only the tip, not the whole bill. The corrected version is: share = (85 + 85 * 18 / 100) / 4. Here, the parentheses ensure the total (bill + tip) is computed first: 85 + 15.3 = 100.3, then 100.3 / 4 = 25.075, or about $25.08 per person.Question 5: CRITICAL THINKING
(2 + 3) * 4 == 2 + 3 * 4. Without running it in Python, predict the output and explain why. Then explain why this demonstrates that parentheses and operator precedence are tools every programmer must master — even when working with operators beyond arithmetic.Show answer
(2 + 3) * 4 = 5 × 4 = 20. The right side computes 2 + 3 * 4 = 2 + 12 = 14. Since 20 ≠ 14, the == comparison evaluates to False. This example demonstrates that arithmetic operators are evaluated before comparison operators (== has lower precedence than + or *). It also shows that parentheses can fundamentally change the meaning of an expression. In real programs, misunderstanding precedence in conditions can cause bugs in if-statements, loops, and data processing — making this knowledge essential for all of programming, not just math calculations.SUMMARY
Lesson Summary
Summary
Python provides seven arithmetic operators: addition (+), subtraction (−), multiplication (*), true division (/), floor division (//), modulo (%), and exponentiation (**). These operators follow a strict precedence hierarchy: parentheses first, then exponentiation, then multiplication/division/floor division/modulo, and finally addition/subtraction.
When operators share the same precedence level, Python uses left-to-right associativity for all arithmetic operators except **, which uses right-to-left associativity. True division (/) always returns a float, even with integer operands. The golden rule: use parentheses liberally to make your expressions clear, correct, and easy for others to read.