Concepts / Operators and Their Precedence

Operators and Their Precedence

An expression is a combination of values, variables, and operators, or a single value or variable by itself.

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

Expressions Begin with Small Pieces

An expression is a combination of values, variables, and operators, or it can be a single value or variable by itself. This means expressions are not limited to long calculations. The value 42 is an expression, and the variable name score is also an expression. These simple expressions are the building blocks for larger computations.

totalvariable operand*operator2value operandcombined withacts on
How do values, variables, and operators fit together to form an expression?

In the expression total * 2, total and 2 are operands: they are the pieces the operation uses. The asterisk is the operator: it tells the program what operation to perform. A compound expression therefore combines operands and an operator into something that produces a result.

From Variable to Result

When a variable appears in an expression, the program substitutes the variable's current stored value before evaluating the expression. For example, if a is 8 and b is 3, the expression a + b is treated as 8 + 3 during evaluation, producing 11.

a + bexpression8 + 3current variable valuesAdditionapply operator11resultreplace variablesuse valuesproduces
How do variable values move into an expression, and how is a result produced?
Worked example

Evaluating a + b

If a is 8 and b is 3, evaluate the expression a + b.

  1. 1
    Identify the pieces

    a and b are variables, and + is the operator.

  2. 2
    Substitute current values

    Replace a with 8 and b with 3, giving 8 + 3.

  3. 3
    Apply the operator

    Apply the addition operation to the substituted values.

Result

The expression evaluates to 11.

Precedence in Compound Expressions

Precedence is the question of which operation is applied first when a compound expression contains more than one operator. The source material establishes the essential evaluation process: identify the operands, substitute current variable values, apply the operators, and obtain a result. It does not provide a language-specific precedence table or state which of several different operators must be applied first.

valueoperandoperatorone operationvalueoperandoperatoranother operationvalueoperandprecedencedetermines operation orderorder may matterorder may matter
What changes when an expression contains multiple operators with different precedence levels?

Results Do Not Rewrite Variables

Evaluating an expression produces a result, but it does not change the original variable unless you explicitly assign the result back. If score appears in score + 10, the expression uses the current value of score and produces a new result. The expression itself does not update score.

Worked example

Using a Variable Without Changing It

Suppose score stores 25. Consider the expression score + 10.

  1. 1
    Read the variable

    The current value stored in score is 25.

  2. 2
    Substitute the value

    The expression becomes 25 + 10.

  3. 3
    Evaluate

    The expression produces a new result by applying the plus operator.

  4. 4
    Check the original variable

    Evaluating the expression alone does not change the value stored in score.

Result

The expression produces 35, while score remains unchanged unless the result is explicitly assigned back.

Mistakes During Evaluation

  • Common mistake

    Thinking only multi-part calculations are expressions

    What it looks like

    Treating 42 or score as something other than an expression

    Why it fails

    A single value or a single variable name qualifies as an expression.

    Do this instead

    Recognize bare values and variable names as the simplest expressions.

  • Common mistake

    Skipping variable substitution

    What it looks like

    Trying to evaluate a + b without replacing a and b with their current stored values

    Why it fails

    The program substitutes each variable's current value before evaluating the expression.

    Do this instead

    Write the substituted expression first, then apply its operator.

  • Common mistake

    Treating an expression's result as an automatic variable update

    What it looks like

    Assuming score + 10 changes score merely because score appears in the expression

    Why it fails

    An expression produces a result but does not change the original variable by itself.

    Do this instead

    Distinguish evaluating an expression from explicitly assigning its result back.

  • Common mistake

    Guessing a precedence order without checking the language rules

    What it looks like

    Assuming the first operator written is always applied first in a multi-operator expression

    Why it fails

    The supplied material does not provide a precedence table, so a specific ordering cannot be concluded from it.

    Do this instead

    Use the relevant programming language's documented operator rules.

Practice the Evaluation Process

EASY

Suppose quantity stores 6. Identify the operands and operator in quantity * 2, substitute the variable's current value, and state the result produced by the expression. Then explain whether evaluating the expression alone changes quantity.

Hints
  • The variable is one operand, and 2 is the other operand.
  • Replace quantity with its current value before applying the operator.
  • Separate the produced result from any explicit assignment back to quantity.
Worked example

Practice Solution

Evaluate quantity * 2 when quantity stores 6.

  1. 1
    Identify the parts

    quantity and 2 are operands, and * is the operator.

  2. 2
    Substitute

    Replace quantity with 6, giving 6 * 2.

  3. 3
    Apply the operator

    Apply the multiplication operation to the substituted values.

  4. 4
    Check the variable

    The expression produces a result but does not change quantity by itself.

Result

The expression produces 12, and quantity remains unchanged unless the result is explicitly assigned back.

Key Takeaways

  1. 1
    An expression can be a combination of values, variables, and operators, or a single value or variable.
  2. 2
    Variables in expressions are replaced by their current stored values before evaluation.
  3. 3
    Operators act on operands and produce a result.
  4. 4
    Evaluating an expression does not change an original variable unless the result is explicitly assigned back.
  5. 5
    Precedence concerns the order of operations in a multi-operator expression; the exact rules must come from the programming language being used.

At a glance

  • Expressions range from a single value or variable to a compound combination of operands and operators.
  • Evaluation substitutes each variable with its current stored value before applying operators.
  • A compound expression produces a result, but the original variables remain unchanged unless explicitly updated.
  • Operator precedence determines operation order in expressions with multiple operators, but specific precedence rankings depend on language rules not supplied here.

Related concepts

More concepts starting with O