Variables, Types & Operators · Lesson 16

Arithmetic Operators and Precedence

Arithmetic Operators and Precedence describes how Python combines or compares values.

ConceptWorked examplePracticeKnowledge check
Textbook walkthrough

What Arithmetic Operators and Precedence actually means

Arithmetic Operators and Precedence describes how Python combines or compares values. Operators form expressions; Python evaluates them according to precedence and associativity rules, producing a new value or updating a binding for augmented assignment.

Arithmetic Operators and Precedence matters because every later calculation, condition and function operates on named objects with specific types. A wrong binding, conversion or operator can silently change the meaning of a program long before an obvious error appears.

Deeper walkthrough

Read Arithmetic Operators and Precedence as a mechanism, not a recipe

Treat this as a sequence of observable decisions rather than one opaque command. Stage 1: Evaluate operands. Stage 2: Apply higher-precedence operations before lower-precedence operations unless parentheses override the order. Stage 3: Comparison operators produce booleans. Final checkpoint: Augmented assignments such as += combine an operation with assignment.

Mechanism

Follow the transformation

Evaluate operands.

Apply higher-precedence operations before lower-precedence operations unless parentheses override the order.

Comparison operators produce booleans.

Evidence

Know what would convince you

  • Run the operation on a tiny literal input and write the expected type/value before executing it.
  • Inspect the relevant object state before and after the operation, especially when mutable objects are involved.
Useful distinction+ - * / // % **: Arithmetic and numeric transformations.
Click a stage to inspect what happens, what changes, and what should be checked before moving on.
Stage 1

Evaluate operands

Evaluate operands. For Arithmetic Operators and Precedence, identify the exact state before this stage, the operation or rule applied here, and the observable state afterwards so the mechanism remains inspectable.

State focus: identify exactly what changed at this stage and what observable evidence confirms that change.
How it works

Trace the mechanism step by step

  1. Evaluate operands.
  2. Apply higher-precedence operations before lower-precedence operations unless parentheses override the order.
  3. Comparison operators produce booleans.
  4. and/or use short-circuit evaluation, so the second operand may not be evaluated.
  5. Augmented assignments such as += combine an operation with assignment.
Worked demonstration

Make the concept concrete

Demonstration

Python example

# Step 1 — Compute the right-hand expression and store its result in `a` for the next step.
a = 10
# Step 2 — Compute the right-hand expression and store its result in `b` for the next step.
b = 3
# Step 3 — Display the current value explicitly so the result/state can be inspected during execution.
print(a / b)
# Step 4 — Display the current value explicitly so the result/state can be inspected during execution.
print(a // b, a % b)
# Step 5 — Display the current value explicitly so the result/state can be inspected during execution.
print(2 + 3 * 4, (2 + 3) * 4)
# Step 6 — Display the current value explicitly so the result/state can be inspected during execution.
print(a > 5 and b < 5)
Expected / illustrative result
3.3333333333333335
3 1
14 20
True
Interpret the result.

For Arithmetic Operators and Precedence, connect the displayed result to the specific input and mechanism above; independently verify one value/state change rather than treating successful execution as proof.

Distinctions & related ideas

Know what this is — and what it is not

+ - * / // % **Arithmetic and numeric transformations.
== != < <= > >=Comparisons that produce booleans.
and / or / notLogical composition; and/or short-circuit.
+= -= *= ...Update through an operation followed by assignment.
Use deliberately

When it is appropriate

Use Arithmetic Operators and Precedence when the program genuinely needs this language behaviour and you can state the input object, resulting value/state and expected failure behaviour.

Boundary conditions

When to stop or reconsider

Choose a clearer built-in, data structure or control-flow pattern when it expresses the intent more directly; stop if implicit conversion, mutation or hidden state makes the behaviour hard to reason about.

Common mistakes

Failure modes to recognise

  • Applying the operation to an incompatible type or assuming Python will silently coerce values the way you intended.
  • Confusing a returned value with an in-place mutation or other side effect.
  • Testing only the happy path and missing empty, boundary or invalid inputs.
Verification

How to check the result

  • Run the operation on a tiny literal input and write the expected type/value before executing it.
  • Inspect the relevant object state before and after the operation, especially when mutable objects are involved.
  • Try one boundary or invalid input and confirm that the returned value or exception matches the intended contract.
Hands-on practice

Demonstrate understanding

Try this:

Build a tiny, inspectable example of Arithmetic Operators and Precedence. First evaluate operands. Then apply higher-precedence operations before lower-precedence operations unless parentheses override the order. Write the expected result before running it, and explain one condition that would make the result misleading or invalid.

Use the smallest values that expose the rule. Write the expected value and type first, then compare Python’s actual state/output with that prediction.
Knowledge check

Check reasoning, not memorisation

Before trusting a result from Arithmetic Operators and Precedence, which check provides the strongest evidence that you understand and applied it correctly?

Quick reference

Keep the important distinctions visible

Step 1Evaluate operands.
Step 2Apply higher-precedence operations before lower-precedence operations unless parentheses override the order.
Step 3Comparison operators produce booleans.
Step 4and/or use short-circuit evaluation, so the second operand may not be evaluated.
Lesson summary

What to remember

  • Arithmetic Operators and Precedence describes how Python combines or compares values. Operators form expressions; Python evaluates them according to precedence and associativity rules, producing a new value or updating a binding for augmented assignment.
  • Evaluate operands.
  • Applying the operation to an incompatible type or assuming Python will silently coerce values the way you intended.
  • Run the operation on a tiny literal input and write the expected type/value before executing it.