Combining Literals Top Variables Contents

Precedence and Associativity

Precedence

Precedence (partially) describes the order in which operators, in an expression involving different operators, are evaluated. In Python, the expression

    3 + 4 < 10 - 2

evaluates to true. In particular, 3 + 4 and 10 - 2 are evaluated before the <, yielding 7 < 8, which is indeed true. This implies that + and - have higher precedence than <. If < had higher precedence, then 4 < 10 would be evaluated first, yielding 3 + True - 2, which is nonsensical.

Note that precedence is only a partial ordering. We cannot tell, for example whether 3 + 4 is evaluated before the 10 - 2, or vice versa. Upon close examination, we see that it does not matter which is performed first as long as both are performed before the expression involving < is evaluated.

It is common to assume that the left operand is evaluated before the right operand. For the Boolean connectives and and or, this is indeed true. But for other operators, such an assumption can lead you into trouble. You will learn why later. For now, remember never, never, never depend on the order in which operands are evaluated!

The lowest precedence operator in Python is the assignment operator which is described later. Next come the Boolean connectives and and or. At the next higher level are the Boolean comparatives, <, <=, >, >=, ==, and !=. After that come the additive arithmetic operators + and-. Next comes the multiplicative operators *, / and %. Higher still is the exponentiation operator **. Finally, at the highest level of precedence is the selection, or dot, operator (the dot operator is a period or full-stop). Higher precedence operations are performed before lower precedence operations. Functions which are called with operator syntax have the same precedence level as the mathematical operators.

Associativity

Associativity describes how multiple expressions connected by operators at the same precedence level are evaluated. All the operators, with the exception of the assignment and exponentiation operators, are left associative. For example the expression 5 - 4 - 3 - 2 - 1 is equivalent to ((((5 - 4) - 3) - 2) - 1). For a left-associative structure, the equivalent, fully parenthesized, structure has open parentheses piling up on the left. If the minus operator was right associative, the equivalent expression would be (5 - (4 - (3 - (2 - 1)))), with the close parentheses piling up on the right. For a commutative operator, it does not matter whether it is left associative or right associative. Subtraction, however, is not commutative, so associativity does matter. For the given expression, the left associative evaluation is -5. If minus were right associative, the evaluation would be 3.

lusth@cs.ua.edu


Combining Literals Top Variables Contents