If you use multiple operators in a single expression, the semantics of that expression depends on the assumed operator precedence. For example, consider the expression
2 + 4 * 0. Does Python calculate
(2 + 4) * 0 or
2 + (4 * 0)? Depending on the operator precedence, the result would be 0 or 2.
The following code shows the correct precedence for this example:
>>> 2 + 4 * 0 2 >>> (2 + 4) * 0 0 >>> 2 + (4 * 0) 2
To avoid any confusion, Python formally defines the operator precedence of all operators. Let’s have a look at the table next!
Python Operator Precedence Table
The following table describes Python’s operator precedence relationship, from highest to lowest precedence.
Operators in the same row have the same precedence. For example, comparisons, membership, and identity have the same precedence. In case of a conflict, they have a left-to-right precedence resolution scheme. In other words, the left operator has precedence over the right operator if both have the same theoretical precedence level.
|Multiplication, Division, Modulo, Integer Division|
|Bitwise Shift Right and Left|
|Bitwise XOR and OR|
Python Operator Precedence Examples
Let’s dive into some examples next.
Parentheses Before Multiplication
The first example shows that parentheses take precedence over multiplication:
>>> # Parentheses >>> (2 + 2) * 0 0
Exponentiation Before Multiplication
The next example shows that exponentiation takes precedence over multiplication too:
>>> 2 ** 3 * 2 16
If instead multiplication took precedence over exponentiation, the result would be
2 ** (3 * 2) == 2 ** 6 == 64. But this is not the case!
Same Precedence Level Resolution: From Left to Right
The next example shows that multiplication, division, modulo, and integer division have the same precedence level. In that case, precedence is resolved from left to right!
>>> 2 * 3 // 2 3
Note that in the previous example, the integer division operation didn’t take precedence or Python would have calculated
2 * (3 // 2) == 2 * 1 ==2.
Arithmetic Operations Before Identity
Identity has a pretty low precedence level, so all arithmetic operations take precedence over it. In the following example, you can see that the multiplication operation
2 * 3 takes precedence before the
>>> 2 * 3 is 3 False
The output clearly indicates that Python didn’t calculate
2 * (3 is 3) because this would’ve yielded
2 * True == 2:
>>> 2 * (3 is 3) 2
Identity and Membership Have the Same Precedence Level
Both the identity operator and the membership operator both have the same low hierarchical level in the precedence table.
Here’s an example:
>>> 2 in [1, 2] is [1, 2] False
This means that the resolution was done from left to right:
2 in [1, 2] is [1, 2] == True is [1, 2] == False. Otherwise, Python would’ve raised an error:
>>> 2 in ([1, 2] is [1, 2]) Traceback (most recent call last): File "<pyshell#34>", line 1, in <module> 2 in ([1, 2] is [1, 2]) TypeError: argument of type 'bool' is not iterable
Python Precedence of Logical Operators
nottakes precedence over
andtakes precedence over
The first rule is exemplified in this code snippet where Python computes
(not False) and False == True and False == False and not
not (False and False == not False == True.
>>> not False and False False
The second rule is exemplified in this code snippet where Python computes
True or (False and False) == True or False == True and not
(True or False) and False == True and False == False.
>>> True or False and False True
Of course, it follows from the two above rules that logical
not takes precedence over logical
Python Precedence and Associativity
Associativity defines the order in which an expression with multiple operators of the same precedence level are evaluated. For example, multiplication and integer division have the same precedence level. Python resolves this by going from left to right in the expression and evaluating the left operator first and the right operator second.
The following example shows that Python first evaluates the multiplication
2 * 3 and only then applies the integer division
6 // 2 == 3.
>>> 2 * 3 // 2 3
Note that the output would’ve been different if Python had evaluated
3 // 2 first which equals
1. The multiplication
2 * 1 yielded
2 which is not what we’ve seen in the code output.
Python Precedence Chart
The following precedence chart from here shows the precedence table in an easy-to-grasp
Where to Go From Here?
Enough theory, let’s get some practice!
To become successful in coding, you need to get out there and solve real problems for real people. That’s how you can become a six-figure earner easily. And that’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?
Practice projects is how you sharpen your saw in coding!
Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people?
Then become a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.
Join my free webinar “How to Build Your High-Income Skill Python” and watch how I grew my coding business online and how you can, too—from the comfort of your own home.
While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.
To help students reach higher levels of Python success, he founded the programming education website Finxter.com. He’s author of the popular programming book Python One-Liners (NoStarch 2020), coauthor of the Coffee Break Python series of self-published books, computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.
His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.