Concepts / Mathematical Operators in Python

Mathematical Operators in Python

Order of operations (PEMDAS) determines which calculations happen first: Parentheses, Exponentiation, Multiplication/Division, Addition/Subtraction.

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

beforebeforebeforeParenthesesHighest precedenceExponentiation**Multiplication andDivisionLeft to rightAddition andSubtractionLeft to right
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.

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

Multiplication and division together

Evaluate 24 / 6 * 2.

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

Choose the leftmost operation: Division appears before multiplication, so evaluate 24 / 6 first. The intermediate value is 4.

Continue with the remaining operation: The expression is now 4 * 2, which evaluates to 8.

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.

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

Applying several precedence levels

Evaluate 5 + 2 ** 2 * 3.

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

Rewrite the expression: The expression becomes 5 + 4 * 3.

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

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

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.

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

Changing the grouping

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

Evaluate the expression without added parentheses: Multiplication takes precedence, so 3 * 4 is evaluated first. The result is 2 + 12, or 14.

Evaluate the parenthesized expression: The parentheses require 2 + 3 to be evaluated first. The result is 5 * 4, or 20.

Compare the outcomes: Adding parentheses changed the grouping and therefore changed the result.

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

Mistakes Beginners Make

  • Evaluating every expression strictly from left to right

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

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

  • Assuming multiplication always happens before division

    Multiplication and division share the same precedence.

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

  • Applying left-to-right order across different precedence levels

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

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

  • Avoiding parentheses because the default result seems obvious

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

    Fix: 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. 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.