Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Exploring Operator Precedence in Python

00:00 What is operator precedence? When evaluating multiple operations, Python must decide the order somehow. Like in math, certain operations have precedence over others, and this ultimately determines which expressions are resolved first. We’ll start with a quick illustration.

00:17 Open the REPL and try the following expression, but don’t hit Enter right away. 20 + 4 * 10. And what should this evaluate to? Without some set of rules for operator precedence, there’s ambiguity.

00:30 20 + 4 is 24, and 24 * 10 is 240, but alternatively, 4 * 10 is 40, and 40 + 20 is 60. So what do you think? Hit Enter and see the result?

00:45 It’s 60. The latter of the two options, and this would make sense if you’re familiar with arithmetic’s order of operations. And if you are, then you already know about half of Python’s rules.

00:58 Speaking of rules, here they are. This table represents all of the major operators in Python, sorted by precedence. Where multiple operators are on the same line, they have equal precedence and are evaluated left to right.

01:09 Starting at the top, resolved first will be any exponents with the ** operator. Then the unary operators + and - apply to their operands.

01:18 Multiplication, division, floor division, and modulo are at the next level of precedence. After that comes addition and subtraction. Then the comparison operators, as well as identity and membership operators, resolve.

01:31 The Boolean operators come next with their own order. Boolean not supersedes Boolean and, which takes precedence over Boolean or, and finally, the walrus operator goes last.

01:42 And there’s one more rule. Anything inside parentheses is resolved before anything outside of parentheses, meaning you can explicitly decide which operations occur first.

01:51 Again, just like in math.

01:55 Now we can look at a few more examples.

01:59 2 * 3 ** 4 * 5 Before hitting Enter, think about it. In our order of operations, exponents come first. So while it’s the second operation going left to right, 3 ** 4 will resolve first to 81, which will then be multiplied by 2 and then 5, resulting in 810.

02:19 And again, using parentheses, you can ensure certain operations happen first. So if you really meant to raise 2 * 3, which is 6, to the power of 4 * 5, which is 20, you can do it like this: (2 * 3)in parentheses, to the power of ** (4 * 5) in parentheses,

02:39 resulting in 3,656,158,440,062,976,

02:49 which is a bit more than 810. So while that was an example of using parentheses to explicitly manipulate the order of operations, there’s also nothing wrong with using parentheses even when they’re redundant.

03:02 For example, create the variables a and b, assigning 5 to a and 50 to b,

03:08 and try the following: (a < 10) and (b > 30). This returns True because both subexpressions are true, but due to precedence rules, this is equivalent to the same code without the parentheses: a < 10 and b > 30, which of course is also True.

03:31 So is one version more correct? Not really. It’s a valid style to add these unnecessary parentheses, especially if it makes the code easier for you or your team to read.

03:43 So there it is. At this point, you have all the skills you need to write some examples on your own and explore this concept further. Play around with it. For my part, I think it’s time to bring this course to a close in the summary.

Become a Member to join the conversation.