Concepts / Mathematical Operators in Python

Mathematical Operators in Python

When Python evaluates a mathematical expression, it does not simply perform every operation from left to right.

  • Programming
Interactive lab

Try it: Order of Operations

How Python decides which operator in an expression runs first — precedence, left-to-right order and right-to-left **, plus what /, // and % really return.

How it works

  1. Parentheses first; then **, which groups right to left and binds tighter than a minus sign on its left.
  2. Then *, /, // and %, left to right; then + and −, left to right.
  3. / always gives a float; // rounds down; % takes the sign of the divisor.
  4. Each step replaces one operation with its value until one number is left.

Default run (7 steps): Expression: 2 + 3 * (7 - 4) ** 2 // 5. Python evaluates left to right, but higher-precedence operators bind first. … Result: 7 (an int).

Simplified: Numbers (at most 6 digits) and arithmetic operators only; parsed and evaluated by the lab's own evaluator (no eval), checked against real Python results.

Educational simulation

Loading the simulation…

Why Evaluation Order Matters

When Python evaluates a mathematical expression, it does not simply perform every operation from left to right. It follows an order of operations so that an expression has one consistent meaning. The key rule is to check precedence first: higher-priority operations are evaluated before lower-priority operations. Left-to-right order matters when the operations being considered share the same precedence level.

What do you think happens?

What result should you expect from the expression 2 + 3 * 4?

  • 20

  • 14

  • 24

Reveal answer
Answer:

14

Multiplication has higher precedence than addition, so 3 * 4 is evaluated first. The expression then becomes 2 + 12, which produces 14.

The PEMDAS Hierarchy

PEMDAS gives the precedence order from highest to lowest: Parentheses, Exponentiation, Multiplication and Division, then Addition and Subtraction. An operation at a higher level is evaluated before an operation at a lower level, even when the lower-level operation appears earlier in the expression.

ParenthesesHighest precedenceExponentiation**Multiplication andDivisionLeft to rightAddition andSubtractionLeft to rightbeforebeforebefore
What is the order in which Python evaluates parentheses, exponentiation, multiplication or division, and addition or subtraction?
Precedence levelOperationsTie-breaking direction
1ParenthesesThe expression inside is evaluated first
2ExponentiationEvaluated before multiplication, division, addition, or subtraction
3Multiplication and divisionLeft to right
4Addition and subtractionLeft to right

PEMDAS lists the priority levels. Left-to-right evaluation is used when operations share a level.

Resolving Equal-Priority Operations

Multiplication and division share one precedence level. Python evaluates them from left to right, choosing whichever of those operations appears first. Addition and subtraction follow the same rule at their own level. Left-to-right is therefore a tie-breaker, not the main rule for the entire expression.

8 / 2 * 3Original expression8 / 244 * 31212first operationreplace with 4evaluate
When multiplication and division appear together, which operation happens first, and how does its intermediate result change the next calculation?
Worked example

Multiplication and division together

Evaluate 24 / 6 * 2.

  1. 1
    Identify the precedence level

    Division and multiplication have the same precedence, so neither automatically jumps ahead of the other.

  2. 2
    Choose the leftmost operation

    Division appears before multiplication, so evaluate 24 / 6 first. The intermediate value is 4.

  3. 3
    Continue with the remaining operation

    The expression is now 4 * 2, which evaluates to 8.

Result

24 / 6 * 2 evaluates to 8.

Tracing a Complete Expression

To evaluate a longer expression, repeatedly find the highest-precedence operation that remains. Resolve parentheses first, then exponentiation, then multiplication or division from left to right, and finally addition or subtraction from left to right. Each resolved operation changes the expression into a simpler one.

2 + 3 * 4Start2 + 123 * 4 = 12142 + 12resolve multiplicationresolve addition
What happens to a complete expression after each precedence level is resolved, and what intermediate value is produced at each step?
Worked example

Applying several precedence levels

Evaluate 5 + 2 ** 2 * 3.

  1. 1
    Evaluate exponentiation

    Exponentiation has higher precedence than multiplication and addition. Evaluate 2 ** 2 to get 4.

  2. 2
    Rewrite the expression

    The expression becomes 5 + 4 * 3.

  3. 3
    Evaluate multiplication

    Multiplication has higher precedence than addition. Evaluate 4 * 3 to get 12.

  4. 4
    Evaluate addition

    The expression becomes 5 + 12, which evaluates to 17.

Result

5 + 2 ** 2 * 3 evaluates to 17.

Parentheses as Control and Clarity

Parentheses have the highest precedence and cause the expression inside them to be evaluated first. They can change the result by grouping operations differently. They can also make the intended grouping obvious even when the result would be unchanged without them.

2 + 3 * 4Multiplication first(2 + 3) * 4Addition first1420evaluateevaluate
How does adding parentheses change which part of an expression Python evaluates first?
Worked example

Changing the grouping

Compare 2 + 3 * 4 with (2 + 3) * 4.

  1. 1
    Evaluate the expression without added parentheses

    Multiplication takes precedence, so 3 * 4 is evaluated first. The result is 2 + 12, or 14.

  2. 2
    Evaluate the parenthesized expression

    The parentheses require 2 + 3 to be evaluated first. The result is 5 * 4, or 20.

  3. 3
    Compare the outcomes

    Adding parentheses changed the grouping and therefore changed the result.

Result

2 + 3 * 4 evaluates to 14, while (2 + 3) * 4 evaluates to 20.

Mistakes Beginners Make

  • Common mistake

    Evaluating every expression strictly from left to right

    What it looks like

    Treating 2 + 3 * 4 as though 2 + 3 happens before multiplication

    Why it fails

    Precedence must be checked before left-to-right order. Multiplication has a higher precedence than addition.

    Do this instead

    Evaluate 3 * 4 first, then add 2, producing 14.

  • Common mistake

    Assuming multiplication always happens before division

    What it looks like

    Treating 24 / 6 * 2 as though 6 * 2 must happen first

    Why it fails

    Multiplication and division share the same precedence.

    Do this instead

    Evaluate the leftmost operation at that precedence level: 24 / 6 first, then multiply the intermediate result by 2.

  • Common mistake

    Applying left-to-right order across different precedence levels

    What it looks like

    Using left-to-right order to evaluate addition before exponentiation or multiplication

    Why it fails

    Left-to-right is the tie-breaker for operations at the same precedence level, not a replacement for PEMDAS.

    Do this instead

    Use PEMDAS first, then use left-to-right within the multiplication and division level or within the addition and subtraction level.

  • Common mistake

    Avoiding parentheses because the default result seems obvious

    What it looks like

    Writing a longer expression without showing which calculation is intended as a group

    Why it fails

    The expression may be interpreted according to precedence rules in a way that does not communicate the writer's intent clearly.

    Do this instead

    Add parentheses to override the default order or to make the intended grouping readable.

Practice and Review

MEDIUM

Predict the result of 10 - 2 * 3 + 4. Identify the precedence level you use first, show the intermediate expression after that operation, and then explain how the remaining addition and subtraction are evaluated.

Hints
  • Multiplication has higher precedence than addition and subtraction.
  • After multiplication is resolved, addition and subtraction share a level and are evaluated from left to right.
  • Do not evaluate the subtraction before resolving the multiplication.
EASY

Rewrite 6 + 4 * 5 with parentheses so that addition is evaluated before multiplication. Then predict how the result differs from the unparenthesized expression.

Hints
  • Place parentheses around the addition.
  • Evaluate the original expression using PEMDAS before comparing it with the parenthesized version.
  1. 1
    Python follows PEMDAS: parentheses, exponentiation, multiplication and division, then addition and subtraction. Higher-precedence operations are evaluated before lower-precedence operations. Multiplication and division share a level, as do addition and subtraction, so operations within either level are evaluated from left to right. Parentheses can override the default order and improve readability. When evaluating an expression, check precedence first and use left-to-right only to resolve ties.

Key Takeaways

  • PEMDAS determines the main evaluation hierarchy: parentheses, exponentiation, multiplication and division, then addition and subtraction.
  • Operations at the same precedence level are evaluated from left to right.
  • Left-to-right order is a tie-breaker, not a rule that overrides precedence.
  • Parentheses can change an expression's result by changing its grouping.
  • Parentheses can also improve readability when they make the intended calculation explicit.

More concepts starting with M