[MUSIC] In the last video we learned how to write simple arithmetic expressions in Python. Now we're going to build up more complicated expressions. We're going to take these simple expressions, use more than one operator to build up compound expressions, right? Let's see how that works. So here we have a simple program in Python that is evaluating a few compound expressions. And, as in the last video, we're going to use Print to get the result of the expression to print out in the output pane, okay? So, let's just run this program, I've got two compound expressions here where I'm printing out the results. Let's look at the first one. It's 3 + 5 + 7 + 27. Python evaluates that and prints out 42, okay? Okay that makes sense, 3 + 5 = 8 + 7 = 15 + 27 = 42. All right [LAUGH] Python does know how to do math, okay? Now let's look at the next one. I can do both addition and subtraction. I can build up these compound expressions with whatever operators side like so I can say 18- 6 + 4, that evaluates to 16 that seems correct as well. Okay, so the point here is I don't have to just use the single operator in my expression. Right I don't have to do 3 + 5 and then figure out what to do and then get that 8 somewhere and then add 7 later, right? I can combine these things together up into one single compound expression, and I can keep adding and subtracting things as much as I'd like, right? These are basic expression where it doesn't really matter how I value it and I am going to get the same answer. It doesn't matter if 3 + 5 + 7 + 27, I can add those in any order that would be great. Wait a minute though what about the second one? Well if I evaluate from left to right which is you know probably what you're used to doing when you look at these expressions, I do 18- 6 and I get 12 + 4, I get 16, and that's what Python says. But what if evaluated it from right to left, I could say 6 + 4 is 10 and then 18- 10 is 8. Clearly that's not what it did, right? So when I have operators that have equivalent precedence, Python evaluates them from left to right and again this is basically what you're used to when you evaluate these mathematical expressions on your own. Now the question is what happens when I have operators of different precedence? All right, so in order to understand that we have to actually understand what Python uses as the rules of precedence. If we go into the Python documentation, we can find this page. And I've given you the link here, but you can find this by searching for operator precedence within the documentation, right? Then you get to this table, and what this table is showing me is basically the order of operations in Python. Things at the top have the lowest precedence and things at the bottom of the table have the highest precedence. Now you note that there's a bunch things in this table that we don't know anything about. So let's focus on the things that we do understand, the basic arithmatical operations, okay? And the way to read this table is that things with higher precedence will be evaluated first. Okay, those operations will happen first and then it will do the operations with lower precedence. If you have a bunch of operations with the same precedence, then Python will evaluate them from left to right. Okay, and so we can see here that plus and minus have the same precedence therefore if you have an arithmetic expression with bunch of additions and subtractions in it, it's going to just evaluate from left to right. But you can also see that if we have multiplication or division, that's going to be evaluated first before we evaluate addition and subtraction. And the higher precedence here it goes to exponentiation, okay and so that's going to happen before even multiplication and division. It's okay if you don't remember all of this. But I want you to be able to find this table if you do need it. Again, go to Python documentation and search for operator precedence. Okay, back to our Python program here. Let's run this second set of expressions and see what happens. Okay. So we have 7 + 3 x 5 here, okay. If I listed this from left to right it would be 7 + 3 = 10 x 5 = 50, but it actually prints out as 22. So following that table before we've learned that multiplication has a higher precedence than addition, so we do the 3 x 5 first, 3 x 5 = 15, then we do 7 = 15 to get 22. Okay? Similarly, you can see there is division and multiplication in the next one 5.5 times 6 integer divide 2 plus 8, okay? I'll let you decide if 24.0 is actually the correct answer here or not okay? And then finally, we have minus 3 raised to the second power. Hm, okay, well what happens here? It could be minus three squared, which would be nine. Or it could be three squared and then put the minus. Okay, so exponentiation has a higher precedence than the unary minus so it does three squared first and then gets minus nine. Okay? Now, I don't like to remember any of this, okay? So even if I do know it, I don't rely upon it. And I suggest that you don't either, right? I think a better way of dealing with this issue is grouping your mathematics expressions with parentheses. Then you control exactly the order in which things get evaluated. So if I really wanted that 7 + 3 x 5 to be addition first, and then multiplication, I simply put parentheses around 7 + 3 and that says do this first. Okay? So we do things in parentheses first, regardless of the operations. That means that this multiplication here happens only after, okay we've added 7 + 3, okay? So we do that, now we get 50, as I originally wanted for instance. I wanted to do 7 + 3 = 10 x 5 = 50, I can also change the order of operations in the next statement, next expression right? I can force the integer divide to happen first, okay, by putting that in, in parentheses. And then, I can force eight to be added to the answer to that first. And then the multiplication will occur, all right? And, and then finally, if I really wanted, you know, minus three. Raised to the second power. I put the minus 3 in parenthesis here. And now it squares minus 3, giving me 9, okay? So, I highly recommend that you not write compound expressions without parenthesis relying on your beliefs about what the order of operations are, okay? It's very easy to forget what the precedence rules are. It's much simpler to just put parenthesis. And force the expression to be evaluated in the way you want it to be evaluated. This has the added benefit of even if you know how it works, someone reading your code immediately knows what you meant as well. Even if they don't really quite understand the rules as well as you do. In this video, we learned how to write more complicated arithmetic expressions where we have multiple operators in a single expression. These compound expressions are great in that they don't require you to have, to simplify your math ahead of time. But rather, you can write the compound expression exactly the way you want to. However,as soon as we have more than one operator we have to worry about the rules of precedence. My strong advice to you is to not worry about the rules of precedence. Instead to use parentheses to group your operations so that they occur in exactly the order that you want them to occur.