1
00:00:04,660 --> 00:00:07,980
In this video, we're going to look at operator precedence.

2
00:00:07,980 --> 00:00:13,780
It's something you'll have learnt about in basic maths class, and we're going to see how it applies to expressions in Python.

3
00:00:13,780 --> 00:00:20,900
Before I start that though, I'm going to delete lines 16 through 21 in our program.

4
00:00:20,900 --> 00:00:23,060
I added those just to show you what the for loop was doing,

5
00:00:23,060 --> 00:00:26,160
but I don't want to leave it as an example of how you should code,

6
00:00:26,160 --> 00:00:31,940
and in fact I'll take this opportunity to delete the for loop as well.

7
00:00:31,940 --> 00:00:36,360
Alright, so our examples so far have been very simple expressions,

8
00:00:36,360 --> 00:00:38,580
but they can be much more complicated.

9
00:00:38,580 --> 00:00:45,300
So consider the following line as I type it, and see if you can work out what will be printed, before you run the program.

10
00:00:45,300 --> 00:00:51,380
So I'm going to type, on line 13, print and in parentheses I'm going to type a + b

11
00:00:51,380 --> 00:00:59,580
divided by 3 minus 4 multiplied by 12.
Then we've got our closing parenthesis.

12
00:00:59,580 --> 00:01:05,080
So if you can figure out what the answer will be, let's actually run the program and see what happens.

13
00:01:05,080 --> 00:01:08,780
You can see we've got the answer: -35.0.

14
00:01:08,780 --> 00:01:12,620
Now if you're surprised that the answer is -35.0 rather than 12,

15
00:01:12,620 --> 00:01:15,000
then maybe you weren't paying attention in school.

16
00:01:15,000 --> 00:01:17,240
Don't worry, your computer isn't broken

17
00:01:17,240 --> 00:01:22,540
and you can reliably perform arithmetic in Python, but like with all programming languages,

18
00:01:22,540 --> 00:01:27,180
you have to understand some basic rules of the arithmetic operators.

19
00:01:27,180 --> 00:01:32,920
Operator precedence is a fancy term for the relative importance given to each of the operators.

20
00:01:32,940 --> 00:01:39,020
If they were all equally important we could just read the expression on line 13 from left to right,

21
00:01:39,020 --> 00:01:50,580
and we would get the result 12: a is 12 + b which is 3, is 15, divided by 3 gives 5, minus 4 leaves 1 times 12 equals 12.

22
00:01:50,580 --> 00:01:56,120
However multiplication and division have
higher precedence than addition and subtraction,

23
00:01:56,120 --> 00:01:59,220
so those operations will be performed first.

24
00:01:59,220 --> 00:02:01,800
Now many programming languages work in the same way.

25
00:02:01,800 --> 00:02:05,040
This isn't just a peculiarity of Python.

26
00:02:05,040 --> 00:02:10,139
So b divided by 3 is 1.0, and 4 times 12 is 48

27
00:02:10,139 --> 00:02:18,200
So the expression, therefore, is evaluated as 12 plus 1.0 take 48, which is -35.0.

28
00:02:18,260 --> 00:02:23,660
Now if you want to make your expressions clear and unambiguous, you can use parenthesis freely,

29
00:02:23,660 --> 00:02:26,600
even when Python doesn't specifically require them.

30
00:02:26,600 --> 00:02:32,900
So, along those lines, we could revise our
example from line 13, and instead type

31
00:02:32,900 --> 00:02:40,960
print parentheses a + and another set of
parentheses and b divided by 3 in those parentheses,

32
00:02:40,960 --> 00:02:48,960
then a minus, then parentheses 4 times 12. Then we've got our two closing right parentheses.

33
00:02:48,960 --> 00:02:52,560
I'll just space that out a little bit so it's a little bit easier to read.

34
00:02:52,560 --> 00:02:59,460
So if you run this code again, we get the identical answer -35.0 printed twice.

35
00:02:59,460 --> 00:03:03,340
So in other words, both these expressions evaluate to the same value.

36
00:03:03,340 --> 00:03:07,760
Now, if we had intended the earlier expression to reduce the result 12,

37
00:03:07,760 --> 00:03:10,300
we'd write it a different way.

38
00:03:10,300 --> 00:03:13,700
We could do print parentheses,

39
00:03:13,700 --> 00:03:18,040
and we're going to add another three parentheses here, so we've got four in total to the left hand side.

40
00:03:18,100 --> 00:03:25,140
Then a + b, closing parenthesis, divided by 3, closing parenthesis,

41
00:03:25,140 --> 00:03:34,200
- 4 closing parenthesis, multiplied by 12, and there we've got our last closing parenthesis at the end of the line.

42
00:03:34,200 --> 00:03:40,100
And now if we run that, we correctly get the result of 12.0,

43
00:03:40,100 --> 00:03:45,040
or, we could revise that because dividing by 3 is higher precedence than subtracting 4.

44
00:03:45,040 --> 00:03:47,760
We can actually remove one set of parentheses.

45
00:03:47,760 --> 00:03:53,820
So what I could do is change that to print, and we want three left parenthesis this time,

46
00:03:53,820 --> 00:04:00,680
and it's going to be a + b, closing parenthesis, divided by 3 - 4,

47
00:04:00,680 --> 00:04:06,000
closing right parentheses, multiplied by 12, and the final closing parenthesis.

48
00:04:06,000 --> 00:04:12,320
If you run the program again now, we get the same answer, 12.0.

49
00:04:12,320 --> 00:04:17,339
Now we could also use variables to hold the intermediate values if we wanted to.

50
00:04:17,339 --> 00:04:29,420
So if I come down here to line 18, I could type c = a + b. On the next line,

51
00:04:29,420 --> 00:04:37,300
d = c / 3, and e =  d - 4,

52
00:04:37,300 --> 00:04:43,520
and we can do a print, in parentheses, e multiplied by 12.

53
00:04:43,520 --> 00:04:45,580
Now if we run that we,

54
00:04:45,580 --> 00:04:48,700
we also get the value 12 outputted.

55
00:04:48,700 --> 00:04:53,100
So there will be times when it makes sense to break up an expression into smaller parts, as we've done here.

56
00:04:53,100 --> 00:04:57,860
I think in this example, though, the code's harder to read, and difficult to work out what's going on.

57
00:04:57,860 --> 00:05:02,220
Compare the two previous examples and see which one you think's easier to read.

58
00:05:02,220 --> 00:05:05,480
Okay, so I mentioned learning about operator precedence at school.

59
00:05:05,480 --> 00:05:10,660
Unfortunately, many schools use acronyms to help you remember the order of evaluation,

60
00:05:10,660 --> 00:05:13,140
but the acronyms are ambiguous.

61
00:05:13,140 --> 00:05:16,860
So common acronyms are;

62
00:06:08,420 --> 00:06:13,940
So multiplication and division have equal precedence, which is higher than addition and subtraction,

63
00:06:13,940 --> 00:06:16,755
and addition and subtraction have equal
precedence.

64
00:06:16,760 --> 00:06:23,400
Because multiplication and division are equal, expressions using those operations are evaluated from left to right.

65
00:06:23,440 --> 00:06:30,580
So let's go ahead and on line 23, I'll do a print, left and right parentheses to give us a bit of space.

66
00:06:30,580 --> 00:06:34,820
Then on line 25 I'm going to type print parentheses

67
00:06:34,820 --> 00:06:42,460
and within the parentheses I'm going to top a divided by, then another set of parentheses, b multiplied by a,

68
00:06:42,460 --> 00:06:47,860
closing parenthesis and divided by b, closing parentheses.

69
00:06:47,860 --> 00:06:50,840
and if I run this program,

70
00:06:50,840 --> 00:06:55,040
we get the result 0.1111111111

71
00:06:55,040 --> 00:07:03,500
and that's 12 times 3 is 36, and 12 divided by 36 is 1/3 divided by 3 is 1/9.

72
00:07:03,500 --> 00:07:07,640
If you were taught using those acronyms, make sure you're interpreting them correctly.

73
00:07:07,640 --> 00:07:11,680
Alright, so in the next video we're going to look at the string datatype in more detail.

74
00:07:11,680 --> 00:07:14,820
See you in the next video.

