Expressions and Statements
Operator Precedence: Reading a Calculation Correctly
A computer calculation can contain several operations in one numeric expression. Before calculating, you need to know which values belong together. This matters when you read existing code, translate a formula into Python, or investigate a result that looks reasonable but is wrong.
Grouping comes before calculating
Operator precedence is the set of rules that determines how operators group with their inputs when parentheses do not specify the grouping. An operator with higher precedence binds more tightly than an operator with lower precedence. For ordinary numbers, multiplication therefore groups within addition:
Python writes multiplication as *, division as /, and powers as **. The following table summarizes the arithmetic grouping rules used here. Read the rows from tighter grouping to looser grouping; leading signs receive special attention below.
Feature | Grouping rule | Python example |
|---|---|---|
Parentheses | Apply the same rules inside the enclosed group | (7 + 4) * 3 |
Powers | Group repeated powers from right to left | 3 ** 2 ** 2 |
Multiplication and division | Share a level and group from left to right | 30 / 5 * 2 |
Addition and subtraction | Share a level and group from left to right | 18 - 6 + 2 |
Example: In 7 + 4 * 3, multiplication connects 4 and 3. Their product is 12, so the expression becomes 7 + 12 and gives 19. Starting with 7 + 4 would introduce a different grouping.
Equal precedence and powers
Associativity determines grouping when operators share a precedence level. Multiplication and division group from left to right. Addition and subtraction do too. Multiplication does not automatically come before division.
Example: The expression 30 / 5 * 2 groups as (30 / 5) * 2. It gives 6 * 2, or 12. Grouping it as 30 / (5 * 2) instead gives 3. The second version changes the divisor by placing the product inside parentheses.
Powers have a different rule. Python groups a chain of ** operators from right to left:
Thus, 3 ** 2 ** 2 means 3 ** (2 ** 2). To square the result of the first power instead, write (3 ** 2) ** 2. Do not extend the left-to-right rule to every operator.
Parentheses and signs
Parentheses let you specify the intended grouping directly. Within each group, the usual rules still apply. For nested parentheses, resolve the innermost group before using its value in the surrounding calculation.
A unary minus before a power's base needs care. Python reads -5 ** 2 as -(5 ** 2), producing −25. In (-5) ** 2, the parentheses make the negative value the base, producing 25. Include the sign inside parentheses when the negative value is the intended base.
A signed exponent is allowed directly: 2 ** -3 means 2 ** (-3), producing 0.125. This is why a simple instruction to perform every power before every sign is incomplete.
What to remember
- Determine the grouping before doing arithmetic.
- Multiplication and division share a level; addition and subtraction share another.
- Equal-level arithmetic groups left to right, except chained powers, which group right to left.
- Parentheses make your intended grouping explicit, including a negative base.
Example: Checking a Workshop Charge
A craft workshop charges a setup fee of $12 plus $4 for each student. Six students attend, and three clubs share the total charge equally. The setup fee is charged once for the entire workshop. Use operator precedence, the rules that determine how operations group with their inputs, to check the calculations below.
a) Evaluate 12 + 4 * 6 and state the total workshop charge.
First calculate the combined student charges, because multiplication groups more tightly than addition. The student charges total ________.
Then add the single setup fee.
The total workshop charge is $36. This calculation charges each student once and includes the setup fee once.
b) Another student writes (12 + 4) * 6. Evaluate this expression and explain why it does not represent the workshop charge.
First calculate the sum inside the parentheses. That sum is ________.
The expression then multiplies the entire sum by six.
The expression gives $96. Its grouping multiplies both the setup fee and the student charge by six, effectively including six setup fees. The parentheses are valid Python, but they describe a different charging rule.
c) Write and evaluate one Python expression for the amount each club pays.
Each club pays one-third of the entire workshop charge. Group that complete charge before dividing:
(12 + 4 * 6) / 3
Within the parentheses, perform multiplication before addition. The grouped total is ________.
Now divide the total equally among the three clubs.
Each club pays $12. Multiplying that share by three recovers the full $36 charge. Without the outer parentheses, 12 + 4 * 6 / 3 would give $20 because the division would apply to the student charges before the setup fee was added.
A Python program calculates the cost of renting three helmets. Complete the descriptions using expression and assignment statement, each once.
The right-hand side helmet_fee * 3 is an because evaluating it produces a value.
The complete line total = helmet_fee * 3 is an because it evaluates the right-hand side and stores the result under a name.
A scheduling script models a task's duration with the Python expression 48 / 6 * 2 + 3, where the resulting value is in minutes. Evaluate the expression and give the duration in minutes, to one decimal place.