T-Th 9:05
or
T-Th 11:15
in Olin 155

CS 1110: Introduction to Computing Using Python

Fall 2017

Precedences of Operators

You know that multiplication * takes precedence over addition +. That is, the expression 5 + 4 * 3 is evaluated as if it were parenthesized like this: 5 + (4 * 3).

Mathematics has conventions for precedences of operators in order to reduce the number of parentheses required in writing complex expressions. Some of these conventions are standard throughout the world, such as * over +. Others are not.

Below is a table of precedences for Python operators. Refer to this table, or the one on p. 15 of the text Think Python, if you forget the precedences.

Table of operator precedences

ORDER OPERATORS EXAMPLES
Highest Parentheses ( )
  Exponentiation **
  Unary arithmetic ops +x    –x
  Binary arithmetic ops  *    /    %
  Binary arithmetic ops +    –
  Arithmetic relations <    >    <=    >=
  Equality relations ==   !=
  Logical not not
  Logical and and
Lowest Logical or or

For example, the expression

(n != 0) and (10/n > 2)

can be easily rewritten as

n != 0  and  10/n > 2

because relational operators have precedence over logical and.


Some Guidelines

Keep redundant parentheses to a minimum (but don't sacrifice clarity)

Your goal in writing expressions should be to make them a clear and simple-looking as possible, and this can often be done by eliminating redundant expressions —but you can help the reader by putting more space around operators with less precedence. For example, the two assignment statements shown below have the same meaning, but the second is easier to read:

b = ((x<=y) or (x<=z))
b =  x <= y  or   y <= z

Keep redundant parentheses to a minimum. For example, they are not needed in a return statement within a function body (you will learn about the return statement later). The following two statements are equivalent, but the second is easier to read:

return (x >= -10 and x <= 10)
return x >= -10  and   x <= 10

Use parentheses with and and or for clarity

Java gives and precedence over or, so that b or c and d is equivalent to b or (c and d). This convention is non-standard, and controversial for some. Therefore, when writing sequences of ands and ors, we suggest always putting in parentheses to make the meaning clear.

This is a case where the suggestion to eliminate redundant parentheses is overruled in favor of removing confusion and providing clarity.

Use parentheses for long lines of code

Another important use of parentheses is for extremely long lines of code. It is standard convention to limit each line of code to less than 80 characters (for purposes of readability). However, Python is a "whitespace-significant" language, which means that spaces and indentation have specific meanings and Python and cannot be used frivolously to make the code look pretty.

Fortunately, if an expression is in parentheses, you can break it up across several lines. For example, the following is acceptable Python code:

if (width == 0 and height == 0 and
    color == 'red' and emphasis == 'strong' or
    highlight > 100):

When using parentheses to break up code across multiple lines, it is standard practice to indent the lines so that they start just after the position of the initial parenthesis. This is shown in the example above.


Course Material Authors: D. Gries, L. Lee, S. Marschner, & W. White (over the years)