Concepts / Basic Operators in Python

Basic Operators in Python

Operator precedence is a set of rules that determines which operations Python performs first when multiple operators appear in the same expression.

  • Programming

Why Order Matters

When an expression contains several operators, Python must decide which operation to perform first. It does not simply evaluate every expression from left to right. Instead, it follows operator precedence: a set of rules that determines the order in which operations are performed.

What do you think happens?

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

  • 20, because 2 + 3 is evaluated first
  • 14, because 3 * 4 is evaluated first
Reveal answer

Answer: 14, because 3 * 4 is evaluated first

Multiplication has higher precedence than addition. Python first computes 3 * 4 to get 12, then adds 2 to get 14.

evaluatereplace subexpressionevaluate3 * 4multiplication12intermediate value2 + 12addition14final value
What operation happens first, what intermediate value is produced, and how does that value flow into the next operation?

Precedence Before Left-to-Right Evaluation

Python organizes operators into a precedence hierarchy. Operators with higher precedence are evaluated before operators with lower precedence. Multiplication and division have higher precedence than addition and subtraction, following the same convention commonly used in mathematics.

evaluated beforeAddition andsubtractionlower precedenceMultiplication anddivisionhigher precedence
How are the relevant operators ranked, and which operators does Python evaluate before the others?

Precedence decides which different operators come first. If operators have equal precedence, Python evaluates them from left to right.

Tracing a Longer Expression

Step-by-step evaluation

Evaluate 10 - 2 * 3 + 8 / 4.

Find the higher-precedence operations: Multiplication and division come before subtraction and addition. The expression contains 2 * 3 and 8 / 4.

Evaluate multiplication: Compute 2 * 3 to produce 6.

Evaluate division: Compute 8 / 4 to produce 2. Multiplication and division are handled from left to right when they share the same precedence.

Evaluate the remaining operations: The expression is now 10 - 6 + 2. Subtraction and addition share the lower precedence level, so Python evaluates them from left to right: first 10 - 6, then 4 + 2.

6

replace with 6replace with 2left to rightleft to right2 * 368 / 4210 - 6 + 2lower-precedence operationsremain10 - 644 + 26
How do the intermediate values flow from the higher-precedence operations into the remaining lower-precedence operations?

Parentheses as Explicit Grouping

Parentheses override the default precedence rules. Python evaluates the expression inside parentheses as a single unit first, regardless of which operators it contains. This lets you specify the intended grouping directly instead of asking the reader to reconstruct it from precedence rules.

ExpressionFirst groupingResult
2 + 3 * 43 * 414
(2 + 3) * 42 + 320
groupsgroups2 + 3 * 4143 * 4first(2 + 3) * 4202 + 3first
How does adding parentheses change the grouping, evaluation order, and final result?

Mistakes Beginners Make

  • Reading every expression strictly from left to right

    Multiplication has higher precedence than addition, so Python evaluates 3 * 4 first.

    Fix: Evaluate higher-precedence operations first, then continue with the remaining expression.

  • Ignoring equal-precedence left-to-right evaluation

    Subtraction and addition have equal precedence, so Python evaluates them from left to right.

    Fix: Read the remaining equal-precedence operations from left to right unless parentheses specify another grouping.

  • Assuming parentheses are only decorative

    Parentheses override the default precedence rules and can change which operation happens first.

    Fix: Use parentheses when you want a grouping different from the default or when the intended order should be explicit.

Practice and Takeaways

MEDIUM

Predict the result of 10 - 2 * 3 + 8 / 4 without running it. Write the intermediate expression after evaluating the multiplication and division, then apply the remaining operations from left to right.

Hints
  • Evaluate 2 * 3 and 8 / 4 before subtraction and addition.
  • After replacing those subexpressions with their values, evaluate the remaining subtraction and addition from left to right.
EASY

Compare 5 + 3 * 2 and (5 + 3) * 2. Identify the first operation in each expression and predict both results.

Hints
  • In the first expression, multiplication has higher precedence than addition.
  • In the second expression, the parentheses make addition the first operation.
  1. Operator precedence determines which operations Python performs first. Multiplication and division are evaluated before addition and subtraction. Operators with equal precedence are evaluated from left to right. Parentheses override the default order, can change the result, and make the intended grouping easier to read.

Key Takeaways

  • Python uses precedence rules instead of evaluating every mixed expression strictly from left to right.
  • Multiplication and division are evaluated before addition and subtraction.
  • Operators with equal precedence are evaluated from left to right.
  • Parentheses override default precedence and make grouping explicit.
  • When an expression requires careful thought, parentheses can make the code clearer and easier to maintain.