Python • Syntax and Core Types

Arithmetic Operators & Precedence — Use arithmetic operators and operator precedence

Master how Python evaluates math expressions so your programs compute exactly what you intend.

Code braces with terminal and markup symbols representing Python programming.
Inside this lesson 10 sections 5 questions 5 figures

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

Mathematicians like René Descartes and others began using symbols such as +, −, and × in written formulas, replacing lengthy word descriptions. A standard order for evaluating these symbols became necessary.

1900s • PEMDAS is Widely Taught

The mnemonic PEMDAS (Parentheses, Exponents, Multiplication, Division, Addition, Subtraction) became a standard teaching tool in schools worldwide, giving students a reliable framework for precedence.

1950s–60s • Early Programming Languages

Languages like FORTRAN adopted mathematical precedence rules so that programmers could write formulas that behaved just like equations on paper. Operator precedence became a core part of language design.

1991 • Python Is Created

Guido van Rossum released Python with a clear, readable syntax that includes all standard arithmetic operators and follows the familiar mathematical order of operations, making it especially friendly for beginners.

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.

1

Operators

Symbols like +, -, *, /, //, %, and ** that perform arithmetic on values called operands.
2

Precedence

A ranking system that determines the order in which operators are evaluated. Higher-precedence operators execute before lower-precedence ones.
3

Associativity

When operators share the same precedence, associativity decides evaluation direction. Most Python arithmetic operators associate left-to-right; exponentiation (**) associates right-to-left.
4

Parentheses Override

You can always use parentheses ( ) to force a specific evaluation order. Whatever is inside parentheses is calculated first, regardless of default precedence.
5

Integer vs. Float Results

Python returns an integer when using + - * // % ** on integers (except / which always returns a float). Mixing an int with a float produces a float.
✦

KEY TAKEAWAY

Think of operator precedence like a to-do list with priorities. If your boss says "make coffee, then answer emails, but fire alarms come first," you know the alarm overrides everything else. Similarly, ** (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

Python Arithmetic Operator PrecedenceHigher on the pyramid = evaluated first( )ParenthesesHIGHEST**Exponentiation (right-to-left)* / // %Multiply · Divide · Floor Divide · Modulo (left-to-right)+ −Addition · Subtraction (left-to-right)LOWESTTip: When in doubt, add parentheses to make your intent explicit!
The pyramid shows four tiers of precedence. Parentheses sit at the top because they always override everything else. Below them, exponentiation (**) is evaluated next. The third tier contains multiplication, division, floor division, and modulo — all at equal precedence, resolved left-to-right. Finally, addition and subtraction sit at the base with the lowest precedence.

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.

OperatorNameExampleResultReturn Type
+Addition7 + 310int (if both int)
-Subtraction10 - 46int (if both int)
*Multiplication5 * 630int (if both int)
/True Division7 / 23.5Always float
//Floor Division7 // 23int (if both int)
%Modulo (Remainder)7 % 21int (if both int)
**Exponentiation2 ** 38int (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

Floor division rounds toward negative infinity, not toward zero. So -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.

Evaluating: 2 + 3 * 4 ** 2 − 8 // 32 + 3 * 4 ** 2 − 8 // 3OriginalStep 1: Exponentiation (**) — highest precedence2 + 3 * 16 − 8 // 34²=16Step 2: *, //, % — left to right2 + 48 − 23×16=488//3=2Step 3: +, − — left to right50 − 22+48=504850−2=48Final Answer: 48
This step-by-step trace shows how Python evaluates 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

Exponentiation is the one arithmetic operator that associates right-to-left. This means 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. 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 / 2
    But this expression does NOT compute what we want!
  2. 2

    Step 2 — Trace using precedence (incorrect version)

    Python evaluates division before addition. So it first computes 70 / 3 ≈ 23.33 and 5 / 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. 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) / 2
    Parentheses enforce our intended order of operations.
  4. 4

    Step 4 — Trace the corrected expression

    Innermost parentheses first: 80 + 90 + 70 = 240. Then 240 / 3 = 80.0. Then 80.0 + 5 = 85.0. Finally, 85.0 / 2 = 42.5.
    result = 42.5
  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) / 2 42.5 Always 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.

PitfallExampleWhat You ExpectedWhat Python Does
Forgetting division is higher than addition100 + 50 / 275.0125.0 (divides first: 50/2=25, then 100+25)
Chained exponentiation direction2 ** 3 ** 264 (left-to-right)512 (right-to-left: 3²=9, then 2⁹)
True division returns float10 / 52 (integer)2.0 (float — use // if you want int)
Negative floor division-7 // 2−3−4 (rounds toward negative infinity)
Mixing int and float5 + 2.07 (integer)7.0 (float — Python 'promotes' to float)

Common precedence and type pitfalls

🎯

BEST PRACTICE

When writing real programs, don't rely on memorized precedence rules alone. Use parentheses generously to make your code readable and your intent unmistakable. Writing (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.

ConceptWhat You Know NowWhat Comes Next
OperatorsSeven arithmetic operators: + − * / // % **Comparison (==, !=, <, >), logical (and, or, not), assignment (+=, *=)
PrecedenceParentheses → ** → * / // % → + −Full 18-level Python precedence table including bitwise, shift, and ternary operators
Data Typesint and float results from arithmeticComplex numbers, Decimal for financial math, operator overloading in custom classes
ExpressionsSimple arithmetic expressionsBoolean 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

In the expression 5 + 3 * 2, why does Python return 11 instead of 16? Explain which rule of precedence is at work.
Show answer
Python returns 11 because multiplication has higher precedence than addition. According to the precedence rules, 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

What is the output of the following Python expression? Show each step. 10 + 6 // 4 * 3 - 1
Show answer
Start with the operators that have higher precedence: // 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

Evaluate this expression and explain the role of right-to-left associativity: 3 ** 2 ** 2 + 10 % 3
Show answer
Exponentiation has the highest precedence here and associates right-to-left. So 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

You're writing a tip calculator. The bill is $85, you want to add an 18% tip, and then split the total evenly among 4 friends. A classmate writes: share = 85 + 85 * 18 / 100 / 4 Is this correct? If not, write a corrected version using parentheses and compute the result.
Show answer
This is incorrect. Due to precedence, Python evaluates 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

Consider the expression (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
The left side computes (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.

Varsity Tutors • Python • Arithmetic Operators & Precedence