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.
# 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.
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
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.
Frequently Asked Questions
What is operator precedence?
How do I override operator precedence?
Do logical and comparison operators have different precedence?
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.
More in this topic
Nested Lists in Python
Learn how to create, access, and modify nested lists in Python. Build grids and matrices, and understand how shallow copies affect multi-dimensional data.
List Comprehensions in Python
Master Python list comprehensions to create, filter, and transform lists in a single line. Learn the syntax, common patterns, and when to use a regular loop instead.
Copy Python Lists
Learn how to copy Python lists using slicing, the copy method, the list constructor, and the copy module. Understand shallow vs deep copies.