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?
Multiplication has higher precedence than addition, so 3 * 4 is computed first (12), then 2 + 12 gives 14. If you answered 20, you read strictly left to right — the most common first instinct. Precedence, not position, decides what happens first, and the next section makes the ranking exact.
9–12
3–5
An expression is a math phrase the computer works out to a single value. You do not just read it left to right. Times and divide go before plus and minus, so in 2 + 3 4 the computer does 3 4 first to get 12, then adds 2 for 14. If you want a different order, put parentheses around the part that should go first: (2 + 3) * 4 is 20 instead.
6–8
An expression combines values and operators into one computed value. Beyond + and - you get , /, and three more: // (floor division, which drops the remainder), % (the remainder itself), and (power). They do not all run left to right. Precedence ranks them: first, then , /, //, %, then + and -. Within one rank, work left to right. Parentheses beat every rank — whatever sits inside them is computed first. So 2 + 3 4 is 14, but (2 + 3) 4 is 20.
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.
K–2
Numbers and signs make a little puzzle: 2 + 3. Work it out and you get one number, 5. Some signs go first. Times always comes before plus.
When the puzzle is done, the whole thing becomes one number. The computer keeps that one number and uses it next.
Undergrad
Picture an expression as a tree: operands at the leaves, operators at the internal nodes. Precedence and associativity are precisely the rules that decide which tree a flat line of text denotes, and evaluation is a post-order traversal — reduce each subtree to a value, then apply the operator above it. Operand types propagate upward: mixing int and float promotes to float, / yields a float even for whole operands, and // and % follow floor semantics. Once you see the tree, the apparent ambiguity of 2 + 3 * 4 disappears: the grammar admits exactly one parse, and that parse fixes the value.
Postgrad
Precedence and associativity are surface-syntax conventions that let a linear string denote a unique abstract syntax tree; without them the grammar would be ambiguous. Evaluation is then the denotation of that tree under an interpretation of the operator symbols — a compositional map from subterms to values. Two facts matter early: operators are overloaded, their behaviour selected by operand types at run time, and some are partial — division by zero denotes nothing and raises rather than returning a value. 'Follow the order of operations' is the informal name for computing the denotation of the one parse tree the precedence rules force.
precedence
The fixed ranking that decides which operator runs first. , /, //, % outrank + and -; * outranks those; parentheses outrank everything.
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
2 + 12 - 1
14 - 1
13
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.
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?
A statement is one complete instruction; a program's statements run in order from the top of the file to the bottom, each after the one before it.
How close were you? Grade yourself honestly — it sets your review date.
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?
From the top line to the bottom line, one statement at a time, exactly as written.
How close were you? Grade yourself honestly — it sets your review date.
10.Review from folio 1: put these lines in the order they run.
- print("Ready?")
- answer = input()
- print("Go!")