University of Free Knowledge
QA 76.73 · fol. 4

Recipes That Compute a Value

An expression combines values and operators into a single computed value, evaluated under fixed precedence rules. · 11 min

2 + 3 * 4 is an expression: values joined by operators that the computer reduces to a single value. It does not simply work left to right. Multiplication happens before addition, by fixed rules of precedence, and parentheses override those rules. This is the same order of operations you met in arithmetic, made exact. Learn the ranking once and you can predict the result of any expression before you ever run it.

Guess before you learn

Python evaluates the expression 2 + 3 * 4. What single value does it produce?

THE DEPTH DIAL — the same idea, younger or deeper
9–12

9–12

An expression evaluates to a single value that carries a type. Operators have a precedence and an associativity. In Python, * binds tightest and groups right to left; then unary minus; then , /, //, %; then + and -, each grouping left to right. Parentheses force any grouping you want. Type travels through the operators: true division / always gives a float, so 6 / 2 is 3.0, while // floors the result and % returns the remainder. Reading an expression means finding the one grouping the rules force, then reducing it step by step to a single typed value.

precedence

The fixed ranking that decides which operator runs first. , /, //, % outrank + and -; * outranks those; parentheses outrank everything.

234*+
PLATE I The expression 2 + 3 * 4 as a tree: times binds 3 and 4 together first, then plus adds 2.

Ink That Thinks — guess first; the answer draws itself.
Put the evaluation of 2 + 3 * 4 - 1 into the order the computer performs it. Highest precedence goes first.

  1. Multiply: 3 * 4 = 12, leaving 2 + 12 - 1
  2. Add left to right: 2 + 12 = 14, leaving 14 - 1
  3. Subtract: 14 - 1 = 13
  4. The whole expression is 13
Reorder, then commit.
PLATE II Evaluating step by step: precedence first, then left to right — guess in graphite, truth in ink.
Retrieval Gate — answer before you continue 0 / 4

1.Evaluate 2 + 3 * 4.

2.In 8 - 2 * 3, which operation does Python perform first?

3.Evaluate (2 + 3) * 4.

4.Write an expression for 'a plus b times 2', using no parentheses.

To evaluate an expression by hand, do one operation at a time: find the highest-precedence operation left, perform it, and rewrite what remains. Repeat until a single value is left. The worked example below traces exactly that, and a fading version will ask you to fill the next line yourself.

Evaluate 2 + 3 * 4 - 1 step by step — the steps fade as you master them

1
Which operation has the highest precedence here? Perform it and rewrite the expression.
2 + 12 - 1
2
Only + and - remain, so work left to right. Do the addition.
14 - 1
3
Finish with the subtraction.
13
OPERATORWHAT IT DOESRUNS( )groupingbefore everything**powerfirst* / // %times, divide, floor-divide, remaindernext+ -add, subtractlast
PLATE III Operator precedence, highest first. Anything in parentheses is computed before all of it.
Why is this true?

Why does 2 + 3 * 4 give 14 rather than 20?

Because multiplication has higher precedence than addition, so 3 * 4 is computed first, giving 12, and only then is 2 added, giving 14. Reading strictly left to right would give 20, but precedence, not position, sets the order.

Retrieval Gate — answer before you continue 0 / 5

1.Evaluate 2 ** 3.

2.Evaluate 10 // 3 (floor division).

3.Evaluate 10 % 3 (the remainder).

4.What is the value and type of 7 / 2?

5.Without looking: state the precedence order of +, , and *, and say where parentheses fit.

Note

Order of operations still slippery? The Atelier of Mind drills precedence until reading an expression is automatic.

Practice — new ink and old, interleaved

1.Evaluate 17 % 5.

2.Without looking back: what is a statement, and in what order do a program's statements run?

3.Review from folio 3: what type does input() return?

4.What number does print(6 + 6) show?

5.Which expression evaluates to 20?

6.Review from folio 2: after x = 6 then x = x + x, what is the value of x?

7.Which sentence best describes a computer program?

8.Evaluate 1 + 2 * 3 + 4.

9.Review from folio 1: in what order does the computer run a program's statements?

10.Review from folio 1: put these lines in the order they run.

  1. print("Ready?")
  2. answer = input()
  3. print("Go!")
The Call Slip — search everything Ctrl·K / ⌘K