operator precedence
In Python, operator precedence is the set of rules that decides which operators apply first when a single expression contains several of them. Operators that apply first are said to bind more tightly. Multiplication binds tighter than addition, so 2 + 3 * 4 evaluates to 14 rather than 20. Precedence is what lets you write arithmetic the way you’d write it on paper.
When two operators share a precedence level, Python groups them from left to right. Exponentiation (**) and conditional expressions like a if b else c are the exceptions and group from right to left, so 2 ** 3 ** 2 is 512 rather than 64. Comparisons bind less tightly than arithmetic, which is why x + 1 < y compares the sum against y.
The lowest rung of the ordering belongs to the assignment expression operator (:=), which binds so loosely that (n := len(values) > 0) assigns the comparison result to n instead of the length.
Parentheses outrank every operator, so grouping part of an expression in them overrides the default order. When the grouping of an expression isn’t obvious at a glance, parenthesize it, even where the parentheses are redundant. The Python documentation publishes the full ordering in its operator precedence table.
The stepper below applies these rules one operator at a time, so you can watch an expression collapse in binding order:
Example
Say you’re totaling an order that carries a flat handling fee plus a per-unit price, and the fee should be charged once rather than on every unit:
>>> handling_fee = 5
>>> unit_price = 3
>>> quantity = 4
>>> handling_fee + unit_price * quantity
17
>>> (handling_fee + unit_price) * quantity
32
The first expression multiplies before it adds, so the fee lands on the order once. The second forces the addition to happen first, which folds the fee into every unit and inflates the total. Both expressions use the same operators and operands, so the grouping alone accounts for the difference.
Related Resources
Tutorial
Operators and Expressions in Python
In Python, operators are special symbols, combinations of symbols, or keywords that designate some type of computation. You can combine objects and operators to build expressions that perform the actual computation. So, operators are the building blocks of expressions.
For additional information on related topics, take a look at the following resources:
- Python Operators and Expressions (Course)
- Using the "or" Boolean Operator in Python (Tutorial)
- The Walrus Operator: Python's Assignment Expressions (Tutorial)
- Operator and Function Overloading in Custom Python Classes (Tutorial)
- Operators and Expressions in Python (Quiz)
- Using the Python or Operator (Course)
- Using the "or" Boolean Operator in Python (Quiz)
- Python Assignment Expressions and Using the Walrus Operator (Course)
- The Walrus Operator: Python's Assignment Expressions (Quiz)
By Martin Breuss • Updated Aug. 28, 2026