Operator precedence

4.4 Operator precedence

Learning objectives

By the end of this section you should be able to

  • Describe how precedence impacts order of operations.
  • Describe how associativity impacts order of operations.
  • Explain the purpose of using parentheses in expressions with multiple operators.

Precedence

When an expression has multiple operators, which operator is evaluated first? Precedence rules provide the priority level of operators. Operators with the highest precedence execute first. Ex: 1 + 2 * 3 is 7 because multiplication takes precedence over addition. However, (1 + 2) * 3 is 9 because parentheses take precedence over multiplication.

Operator Meaning
() Parentheses
** Exponentiation (right associative)
*, /, //, % Multiplication, division, floor division, modulo
+, - Addition, subtraction
<, <=, >, >=, ==, != Comparison operators
not

Logical not operator

and

Logical and operator

or

Logical or operator

Table 4.4 Operator precedence from highest to lowest.

Checkpoint

Operator precedence

Concepts in Practice

Precedence rules

Which part of each expression is evaluated first?

1.
x ** 2 + 6 / 3
  1. 6 / 3
  2. x ** 2
  3. 2 + 6
2.
not 3 * 5 > 10
  1. 3 * 5
  2. not 3
  3. 5 > 10
3.
z == 5 and x / 8 < 100
  1. 5 and x
  2. x / 8
  3. 8 < 100

Associativity

What if operators beside each other have the same level of precedence? Associativity determines the order of operations when precedence is the same. Ex: 8 / 4 * 3 is evaluated as (8/4) * 3 rather than 8 / (4*3) because multiplication and division are left associative. Most operators are left associative and are evaluated from left to right. Exponentiation is the main exception (noted above) and is right associative: that is, evaluated from right to left. Ex: 2 ** 3 ** 4 is evaluated as 2 ** (3**4).

When comparison operators are chained, the expression is converted into the equivalent combination of comparisons and evaluated from left to right. Ex. 10 < x <= 20 is evaluated as 10 < x and x <= 20.

Checkpoint

Operation precedence

Concepts in Practice

Associativity

How is each expression evaluated?

4.
10 + 3 * 2 / 4
  1. 10 + (3 * (2 / 4))
  2. 10 + ((3 * 2) / 4)
  3. (10 + 3) * (2 / 4)
5.
2 * 2 ** 2 ** 3
  1. 2 * ((2 ** 2) ** 3)
  2. 2 * (2 ** (2 ** 3))
  3. ((2*2) ** 2) ** 3
6.
100 < x > 150
  1. 100 < x and x < 150
  2. 100 < x or x > 150
  3. 100 < x and x > 150

Enforcing order and clarity with parentheses

Operator precedence rules can be hard to remember. Parentheses not only assert a different order of operations but also reduce confusion.

Checkpoint

Using parentheses

Concepts in Practice

Using parentheses

7.
Consider the example above. Why was the evaluation order different from what the programmer wanted?
  1. Equality has precedence over and.
  2. All operators are evaluated right to left.
  3. Order is random when parentheses aren't used.
8.
Given x = 8 and y = 9, what is the result of the following?
x + 3 * y - 5
  1. 30
  2. 44
  3. 94
9.
Given x = 8 and y = 9, what is the result of the following?
(x+3) * (y-5)
  1. 30
  2. 44
  3. 94

PEP 8 recommendations: spacing around operators

The PEP 8 style guide recommends consistent spacing around operators to avoid extraneous and confusing whitespace.

  • Avoid multiple spaces and an unequal amount of whitespace around operators with two operands.
    Avoid: x= y * 44
    Better: x = y * 44
  • Avoid spaces immediately inside parentheses.
    Avoid: x = ( 4 * y )
    Better: x = (4 * y)
  • Surround the following operators with one space: assignment, augment assignment, comparison, Boolean.
    Avoid: x= y<44
    Better: x = y < 44
  • Consider adding whitespace around operators with lower priority.
    Avoid: x = 5 * z+20
    Better: x = 5*z + 20