Skip to content

Computer Science · Ch 5 — Getting Started with Python

Precedence of Operators

5.9.1

Precedence of Operators

When an expression contains different kinds of operators, Python needs a rule to decide which operator to apply first. That rule is operator precedence: a higher-precedence operator is evaluated before a lower-precedence one.

Unary and binary operators

Most operators we have studied so far are binary operators — they need two operands (for example, 10 + 20). A unary operator needs only one operand, and unary operators have higher precedence than binary operators.

  • The minus - and plus + signs can act both as unary and as binary operators. Written before a single value (-Depth) they are unary; between two values (a - b) they are binary.
  • not is a unary logical operator — it negates the single truth value that follows it.
# minus used as a UNARY operator: negates one operand
value = -depth

# not is a unary operator: negates True
print(not(True))
# Output: False

The precedence table

The table lists the precedence of all operators from highest to lowest:

OrderOperatorsDescription
1**Exponentiation (raise to the power)
2~, +, -Complement, unary plus and unary minus
3*, /, %, //Multiply, divide, modulo and floor division
4+, -Addition and subtraction
5<=, <, >, >=, ==, !=Relational and comparison operators
6=, %=, /=, //=, -=, +=, *=, **=Assignment operators
7is, is notIdentity operators
8in, not inMembership operators
9notLogical operators
10and
11or

Two additional rules complete the picture:

  • Parentheses override precedence. The expression inside ( ) is always evaluated first, whatever operators it contains.
  • Equal precedence is resolved left to right. When two operators have the same precedence, the one appearing first (leftmost) in the expression is applied first.

Worked evaluations

Example — mixed + and *. How will Python evaluate 20 + 30 * 40?

# 20 + 30 * 40
# = 20 + (30 * 40)   Step 1: * has higher precedence than +
# = 20 + 1200        Step 2
# = 1220             Step 3

Example — equal precedence, left to right. How will Python evaluate 20 - 30 + 40? Subtraction and addition have equal precedence, so the leftmost operator (the subtraction) is applied before the addition:

# 20 - 30 + 40
# = (20 - 30) + 40   Step 1: left to right
# = -10 + 40         Step 2
# = 30               Step 3

Example — parentheses force the order. How will Python evaluate (20 + 30) * 40?

# (20 + 30) * 40
# = 50 * 40          Step 1: the () forces + to happen before *
# = 2000             Step 2

Without the parentheses this expression would have been 20 + 30 * 40 = 1220; the brackets changed the answer to 2000. …

Table 5.9Precedence of all operators in Python
Order of PrecedenceOperatorsDescription
1**Exponentiation (raise to the power)
2~ ,+, -Complement, unary plus and unary minus
3* ,/, %, //Multiply, divide, modulo and floor division
4+, -Addition and subtraction
5<= , < , > , >=, == , !=Relational and Comparison operators
6=, %=, /=, //=, -=, +=, *=, **=Assignment operators
7is, is notIdentity operators
8in, not inMembership operators