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.
Name | Operators |
---|---|
Parentheses | () |
Exponentiation | ** |
Bitwise Not | ~ |
Multiplication, Division, Modulo, Integer Division | * / % // |
Addition, Subtraction | + - |
Bitwise Shift Right and Left | >> << |
Bitwise AND | & |
Bitwise XOR and OR | ^ | |
Comparison | <= < > >= |
Equality | <> == != |
In-Place Assignments | = %= /= //= -= += *= **= |
Identity, Membership | is, is not , in, not in |
Logical | not, 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 is
operator:
>>> 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
The three logical operators not
, and
, or
have this exact precedence ordering.
not
takes precedence overand
.and
takes precedence overor
.
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 or
.
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 .jpg
:
Where to Go From Here?
Enough theory. Let’s get some practice!
Coders get paid six figures and more because they can solve problems more effectively using machine intelligence and automation.
To become more successful in coding, solve more real problems for real people. 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?
You build high-value coding skills by working on practical coding projects!
Do you want to stop learning with toy projects and focus on practical code projects that earn you money and solve real problems for people?
🚀 If your answer is YES!, consider becoming 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.
If you just want to learn about the freelancing opportunity, feel free to watch my free webinar “How to Build Your High-Income Skill Python” and learn how I grew my coding business online and how you can, too—from the comfort of your own home.