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.

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.

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

Visual Explanation — The Precedence Pyramid

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.

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.

Python's seven arithmetic operators
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)

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.

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.

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!

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 expressionYour 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
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
Step 3 — Fix with parenthesesWe 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
Step 4 — Trace the corrected expressionInnermost 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
Step 5 — Verify in PythonYou 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

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.

Common precedence and type pitfalls
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)
🎯 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.

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.

From arithmetic to advanced Python expressions
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

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.

Practice Problems

PROBLEM 1 — CONCEPTUAL
In the expression 5 + 3 * 2, why does Python return 11 instead of 16? Explain which rule of precedence is at work.
PROBLEM 2 — BASIC CALCULATION
What is the output of the following Python expression? Show each step. 10 + 6 // 4 * 3 - 1
PROBLEM 3 — INTERMEDIATE
Evaluate this expression and explain the role of right-to-left associativity: 3 ** 2 ** 2 + 10 % 3
PROBLEM 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.
PROBLEM 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.

Lesson 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