Operator Precedence in Python

Learn how Python determines which operator runs first when multiple operators appear in an expression, and how to use parentheses to control evaluation order safely.

5 min read

Python operator precedence is the set of rules that determines which operation Python performs first when an expression contains more than one operator. When you write an expression that mixes addition and multiplication, Python does not blindly evaluate from left to right. It follows a priority system where multiplication runs before addition, exponentiation runs before multiplication, and parentheses override everything else. This system ensures that mathematical expressions produce the expected results without forcing you to wrap every sub-expression in parentheses.

The articles in this section have introduced operators one category at a time, and each article mentioned the precedence rules relevant to that category. This article pulls all those rules together into a single reference. You do not need to memorize every level, but understanding the big picture helps you spot bugs before they happen. The practical goal is to recognize when precedence matters and to use parentheses whenever the evaluation order is not immediately obvious to a reader. The concepts here build directly on arithmetic operators in Python and logical operators in Python, where precedence was first introduced for each operator family.

The precedence hierarchy

Python organizes every operator into a strict hierarchy. The highest level runs first, then the next level, and so on down to the lowest. When two operators share the same level, Python evaluates them from left to right, with two important exceptions: exponentiation groups from right to left, and assignment expressions group from right to left.

The highest precedence belongs to expressions that create values: parenthesized expressions, list displays, dictionary displays, and set displays. Next come subscription and indexing with square brackets, function calls with parentheses, and attribute access with a dot. After that comes the await expression used in asynchronous code. These top levels are rarely a source of confusion because the syntax itself makes the grouping obvious.

The arithmetic operators follow in the order that matches mathematical convention. Exponentiation runs first among arithmetic operators. Unary plus, unary minus, and bitwise NOT come next, which is why negative three squared means negative of three squared rather than negative three squared. Multiplication, true division, floor division, and modulo share the next level and evaluate left to right. Addition and subtraction share the level below that.

pythonpython
# Exponentiation before unary minus, then multiplication
result = -3 ** 2 + 4 * 5
# = -(9) + 20
# = 11
print(result)

Bitwise shift operators come after arithmetic, followed by bitwise AND, bitwise XOR, and bitwise OR, each at its own level. Comparison operators, including equality, ordering, identity, and membership, all share the same level below bitwise operations. This shared level enables chained comparisons where Python evaluates an expression like zero less than x less than ten as two separate comparisons joined by an implicit and.

Logical operator precedence

The logical operators have their own internal hierarchy. The not operator has the highest precedence among the three. The and operator comes next. The or operator has the lowest precedence. This means an expression that mixes not, and, and or evaluates not first, then and, and finally or, unless parentheses specify a different order.

The gap between comparison and logical precedence is important because it is what makes conditional expressions readable without parentheses. When you write an if-statement that checks whether age is at least eighteen and has a ticket, Python evaluates both comparisons first and then combines the resulting booleans with the and operator. You do not need parentheses around each comparison because comparison operators have higher precedence than and.

pythonpython
age = 20
has_ticket = True
 
# Comparisons evaluate first, then AND
if age >= 18 and has_ticket:
    print("Entry granted")

The conditional expression using if and else, the lambda expression, and the assignment expression with colon-equals occupy the lowest precedence levels. Assignment with the single equals sign is the lowest of all, which is why you can write an expression on the right side of an assignment without wrapping it in parentheses. Python evaluates the entire right-hand expression first and then assigns the result.

When to use parentheses

Parentheses are the highest-precedence construct in Python. Anything inside parentheses evaluates before anything outside them, and nested parentheses evaluate from the innermost pair outward. Using parentheses does not slow down your program. Python evaluates the parenthesized sub-expression and discards the parentheses at compile time, so there is no runtime cost to adding clarity.

A good rule for beginners is to use parentheses whenever an expression mixes more than two operator categories. The expression combining multiplication and addition is clear without parentheses because everyone learns that multiplication comes before addition. The expression combining bitwise AND with a comparison is not obvious to most readers and should use parentheses even though Python knows the correct order. The time you save by omitting parentheses is not worth the debugging time when someone misreads your code.

Parentheses also prevent bugs when operator precedence changes between languages. A programmer coming from a language where bitwise operators have different precedence might misread a Python expression that relies on Python's specific ordering. Parentheses make the intent explicit regardless of the reader's background, and explicit intent is always worth the two extra characters.

Rune AI

Rune AI

Key Insights

  • Operator precedence determines the order Python evaluates operators in a compound expression.
  • The hierarchy from highest to lowest: parentheses, exponentiation, unary operators, multiplication and division, addition and subtraction, bitwise shifts, bitwise AND/XOR/OR, comparisons, logical NOT, logical AND, logical OR, assignment.
  • Use parentheses to override precedence and make your intent clear.
  • When two operators have the same precedence, Python evaluates left to right except for exponentiation which goes right to left.
  • Never rely on memory alone for precedence; when the expression is complex, add parentheses.
RunePowered by Rune AI

Frequently Asked Questions

What is operator precedence?

Operator precedence is the set of rules that determines which operator Python evaluates first when multiple operators appear in the same expression. For example, in `2 + 3 * 4`, multiplication has higher precedence than addition, so Python computes `3 * 4` first and then adds 2, yielding 14 instead of 20.

How do I override operator precedence?

Use parentheses to group sub-expressions. Anything inside parentheses is evaluated before operators outside the parentheses. For example, `(2 + 3) * 4` forces addition to run first, yielding 20. Parentheses always have the highest precedence.

Do logical and comparison operators have different precedence?

Yes. Comparison operators like `==` and `<` have higher precedence than logical operators like `and` and `or`. This means `x > 0 and y > 0` is evaluated as `(x > 0) and (y > 0)` without needing parentheses. The `not` operator has higher precedence than both `and` and `or`.

Conclusion

Operator precedence determines which operation runs first in a compound expression. Python follows a predictable hierarchy from parentheses down to assignment. When in doubt, add parentheses. Clear code beats clever code every time.