Order of Operations in Complex Expressions
Operators are special symbols representing computations; operands are the values they act upon.
Reading an Expression
An expression is a computation built from operators and operands. Operators are special symbols that represent computations. Operands are the values that operators act upon. When you can separate an expression into these two parts, you can trace what the computation is doing instead of treating the expression as an unexplained string of symbols.
In the expression 5 + 9, the plus sign is the operator, and 5 and 9 are the operands. The addition operator combines the two operands and produces 14.
Five Arithmetic Operators
Python provides five core arithmetic operators that combine operands to produce new values. Most of these operators act on two operands. Exponentiation raises one operand to the power of another.
| Operator symbol | Arithmetic operation |
|---|---|
| + | Addition |
| - | Subtraction |
| * | Multiplication |
| / | Division |
| ** | Exponentiation |
The same symbol can have different roles depending on its position. A minus sign between two values is a binary operator acting on two operands. A minus sign before one value can be a unary operator that negates that value. For example, -5 is the negation of 5.
Precedence Before Left-to-Right Reading
When an expression contains several operators, do not automatically evaluate it from left to right. Python follows operator precedence, which is a hierarchy that determines which operation is performed first. Parentheses override the hierarchy. Exponentiation is performed before the other arithmetic operators. Multiplication and division come before addition and subtraction.
| Priority | Operations |
|---|---|
| Highest | Parenthesized expressions |
| Next | Exponentiation |
| Next | Multiplication and division |
| Lowest | Addition and subtraction |
The precedence hierarchy described in the source material.
Tracing Each Intermediate Result
Evaluating hour * 60 + minute
Determine which operation is performed first in the expression hour * 60 + minute.
Identify the pieces: The expression contains two operators, multiplication and addition, and three operands, hour, 60, and minute.
Apply precedence: Multiplication has higher precedence than addition, so hour is combined with 60 first.
Form the intermediate result: The multiplication produces a new value. That result takes the place of hour * 60 in the remaining expression.
Finish the expression: The intermediate multiplication result is then combined with minute using addition.
The multiplication is evaluated before the addition. The expression is traced as hour * 60, followed by that result + minute.
Adding parentheses
Compare the evaluation order of 2 + 3 * 4 with (2 + 3) * 4.
Without parentheses: Multiplication has higher precedence than addition, so 3 * 4 is evaluated first. The remaining operation is 2 + that multiplication result.
With parentheses: The expression inside the parentheses, 2 + 3, is evaluated first. Its result is then used as the first operand of multiplication by 4.
Compare the groupings: The symbols are similar, but the parentheses change which operands are combined first.
Parentheses override the default precedence and make addition occur before multiplication.
What do you think happens?
Which operation is evaluated first in 2 + 3 ** 2 * 4?
Reveal answer
Answer: 3 ** 2
Exponentiation is performed before the other arithmetic operators. After that, multiplication has priority over addition.
Debugging Unexpected Results
When an expression produces an unexpected result, do not begin by changing symbols randomly. First identify every operator and operand. Then apply the precedence rules one operation at a time. This reveals whether the problem came from an incorrect assumption about precedence, a typo in an operand, or an incorrect operator choice.
- Write down the operators and operands in the expression.
- Look for parentheses and evaluate the expressions inside them first.
- Find exponentiation before the other arithmetic operations.
- Find multiplication and division before addition and subtraction.
- Replace the operation you evaluated with its intermediate result.
- Continue until only the final result remains.
- Compare each step with the result you expected.
Evaluating every operator from left to right
Python uses operator precedence rather than simply evaluating all operations from left to right. Multiplication is performed before addition.
Fix:
Trace the higher-precedence operation first, then use its result in the remaining expression.Ignoring parentheses
Parentheses force the expression inside them to be evaluated before the rest of the expression.
Fix:
Treat each parenthesized expression as the next computation to resolve.Confusing a unary minus with subtraction
In -5, the minus sign negates one operand; it is not subtracting one value from another.
Fix:
Check whether the minus sign has one operand or two.Skipping the intermediate expression
Jumping directly to a final answer makes it difficult to see which operands were combined first.
Fix:
Write the higher-precedence operation and replace it with an intermediate result before continuing.
Practice and Summary
For each expression, identify the operators and operands, then describe the order in which the operations are evaluated: 8 + 2 * 5, (8 + 2) * 5, and 2 ** 3 + 4.
Hints
- Start by identifying any parentheses.
- Then check for exponentiation.
- Multiplication comes before addition when parentheses do not change the grouping.
- Operators are symbols that represent computations, and operands are the values those operators act upon. The five core arithmetic operators are +, -, *, /, and **. Parentheses override the default hierarchy. Exponentiation comes before multiplication and division, which come before addition and subtraction. To debug a complex expression, identify its parts and trace each operator with its operands until the intermediate results lead to the final result.
Key Takeaways
- Operators represent computations, while operands are the values they act upon.
- The five core arithmetic operators are addition, subtraction, multiplication, division, and exponentiation.
- Parentheses override the normal precedence hierarchy.
- Exponentiation is evaluated before multiplication and division, which are evaluated before addition and subtraction.
- Debugging is easier when you trace each operator, its operands, and the intermediate result it produces.