Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
As you are writing code in Python, there are a lot of opportunities for you to use various expressions. And some of these expressions might have a number of operators and a number of factors. For example, let's say that you have an expression that looks something like this: 5 * 25 + 14 / 2. And you might look at an expression like that, maybe you didn't code it yourself, maybe somebody else did, and you are looking at some code and you see that and you say, hmm, is that 5 times the sum of 25 + 14, with the result of that divided by 2, or is it 5 times 25 plus the result of 14 divided by 2, or some other combination? The answer to this question lies in the subject of operator precedence.
When there are a number of different operators in one expression, which operations get evaluated first? And the results of that operation would then be used in operating on the other operators. So in this case, I happened to know, because I looked it up, that multiplication and division have a higher precedence than addition and subtraction. And so I know that the first thing that's going to happen is 5 * 25 will be evaluated, and then 14 / 2 will be evaluated, and those two results will be added together, giving us the answer of 132, because 5 * 25 is 125, and 14 / 2 is 7.
125 + 7 is 132. If we want a different result, obviously we could use parenthesis. We could say 5 * and put in parenthesis here 25 + 14 / 2, and we can get that different result. I strongly suggest when you are writing your own code that you use the parentheses to explicitly say what it is that you mean to say, that you don't write bare expressions with a lot of different operators like the first one here in this example, that you just don't do that.
If you are reading other people's code and you see that and you want to know what it is that they meant or at least what the result is that they got, then you can refer to the Operator Precedence chart, and I've provided one handy in your exercise files. Let's take a look at what operator precedence looks like in Python. There is no need for you to memorize this, but it's good for you to become at least familiar with its existence and know where to find it when you need to look it up. In this chart, you'll see there's three columns: Associativity, Operators, and Description. Associativity means which way is it evaluated, if you have several of these things from this column? So for example, if you have several Boolean or operators, they'll be evaluated left to right.
If you have several Comparison operators, they'll be evaluated right to left. More often than not, they are left to right in Python. The designers of Python have done a really good job of trying to make this stuff consistent as they possibly can. And where it's not consistent, it's because it makes better sense that way. Starting at the top of the list, we have lambda, and then we have or, and, not. These are the Boolean, or, and, and not. Then we have all of the Comparison operators lumped together in one priority, and those are evaluated right to left.
And then continuing, there is the Bitwise operators or, exclusive or, and and. Then there is the Bitwise shift operators, and then we have addition and subtraction, multiplication, division, and remainder, the Unary operators, the Exponent operator, and then bundled together and right to left again slices, function calls, and attribute references, and then finally, left to right the Binding operator, which is the parentheses when it's not meant to be a tuple and then tuple, lists and dictionaries.
So this is operator precedence in Python. Again, it's much better for you to not rely on this, to use parentheses to say exactly what it is that you mean to say when you are writing complex expressions. When you are reading other people's code that have not been that kind to you, it's good to know that this table exists and where to find it, so that you can read that code and know exactly how it gets evaluated.
Get unlimited access to all courses for just $25/month.Become a member
61 Video lessons · 99862 Viewers
56 Video lessons · 112986 Viewers
71 Video lessons · 81818 Viewers
131 Video lessons · 39262 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.