1
00:00:00,000 --> 00:00:02,990
As you are writing code in Python, there are a lot of opportunities for you to

2
00:00:03,000 --> 00:00:04,990
use various expressions.

3
00:00:05,000 --> 00:00:08,990
And some of these expressions might have a number of operators and a number of factors.

4
00:00:09,000 --> 00:00:12,990
For example, let's say that you have an expression that looks something like

5
00:00:13,000 --> 00:00:18,990
this: 5 * 25 + 14 / 2.

6
00:00:19,000 --> 00:00:21,990
And you might look at an expression like that, maybe you didn't code it

7
00:00:22,000 --> 00:00:23,990
yourself, maybe somebody else did,

8
00:00:24,000 --> 00:00:28,990
and you are looking at some code and you see that and you say, hmm, is that 5

9
00:00:29,000 --> 00:00:35,990
times the sum of 25 + 14, with the result of that divided by 2, or is it 5 times

10
00:00:36,000 --> 00:00:42,990
25 plus the result of 14 divided by 2, or some other combination?

11
00:00:43,000 --> 00:00:47,990
The answer to this question lies in the subject of operator precedence.

12
00:00:48,000 --> 00:00:51,990
When there are a number of different operators in one expression, which

13
00:00:52,000 --> 00:00:54,990
operations get evaluated first?

14
00:00:55,000 --> 00:01:00,990
And the results of that operation would then be used in operating on the other operators.

15
00:01:01,000 --> 00:01:06,990
So in this case, I happened to know, because I looked it up, that multiplication

16
00:01:07,000 --> 00:01:11,990
and division have a higher precedence than addition and subtraction.

17
00:01:12,000 --> 00:01:16,990
And so I know that the first thing that's going to happen is 5 * 25 will be

18
00:01:17,000 --> 00:01:23,990
evaluated, and then 14 / 2 will be evaluated, and those two results will be added

19
00:01:24,000 --> 00:01:30,990
together, giving us the answer of 132, because 5 * 25 is 125, and 14 / 2 is 7.

20
00:01:31,000 --> 00:01:35,990
125 + 7 is 132.

21
00:01:36,000 --> 00:01:39,990
If we want a different result, obviously we could use parenthesis.

22
00:01:40,000 --> 00:01:47,990
We could say 5 * and put in parenthesis here 25 + 14 / 2, and we can get

23
00:01:48,000 --> 00:01:49,990
that different result.

24
00:01:50,000 --> 00:01:54,990
I strongly suggest when you are writing your own code that you use the

25
00:01:55,000 --> 00:01:58,990
parentheses to explicitly say what it is that you mean to say,

26
00:01:59,000 --> 00:02:03,990
that you don't write bare expressions with a lot of different operators like

27
00:02:04,000 --> 00:02:07,990
the first one here in this example, that you just don't do that.

28
00:02:08,000 --> 00:02:10,990
If you are reading other people's code and you see that and you want to know

29
00:02:11,000 --> 00:02:14,990
what it is that they meant or at least what the result is that they got, then

30
00:02:15,000 --> 00:02:18,990
you can refer to the Operator Precedence chart, and I've provided one handy in

31
00:02:19,000 --> 00:02:20,990
your exercise files.

32
00:02:21,000 --> 00:02:23,990
Let's take a look at what operator precedence looks like in Python.

33
00:02:24,000 --> 00:02:26,990
There is no need for you to memorize this, but it's good for you to become

34
00:02:27,000 --> 00:02:31,990
at least familiar with its existence and know where to find it when you need to look it up.

35
00:02:32,000 --> 00:02:37,990
In this chart, you'll see there's three columns: Associativity, Operators, and Description.

36
00:02:38,000 --> 00:02:42,990
Associativity means which way is it evaluated, if you have several of these

37
00:02:43,000 --> 00:02:44,990
things from this column?

38
00:02:45,000 --> 00:02:48,990
So for example, if you have several Boolean or operators, they'll be

39
00:02:49,000 --> 00:02:50,990
evaluated left to right.

40
00:02:51,000 --> 00:02:55,990
If you have several Comparison operators, they'll be evaluated right to left.

41
00:02:56,000 --> 00:02:58,990
More often than not, they are left to right in Python.

42
00:02:59,000 --> 00:03:02,990
The designers of Python have done a really good job of trying to make this stuff

43
00:03:03,000 --> 00:03:03,990
consistent as they possibly can.

44
00:03:04,000 --> 00:03:07,990
And where it's not consistent, it's because it makes better sense that way.

45
00:03:08,000 --> 00:03:12,990
Starting at the top of the list, we have lambda, and then we have or, and, not.

46
00:03:13,000 --> 00:03:14,990
These are the Boolean, or, and, and not.

47
00:03:15,000 --> 00:03:19,990
Then we have all of the Comparison operators lumped together in one priority, and

48
00:03:20,000 --> 00:03:21,990
those are evaluated right to left.

49
00:03:22,000 --> 00:03:28,990
And then continuing, there is the Bitwise operators or, exclusive or, and and.

50
00:03:29,000 --> 00:03:32,990
Then there is the Bitwise shift operators, and then we have addition and

51
00:03:33,000 --> 00:03:37,990
subtraction, multiplication, division, and remainder, the Unary operators,

52
00:03:38,000 --> 00:03:39,990
the Exponent operator,

53
00:03:40,000 --> 00:03:44,990
and then bundled together and right to left again slices, function calls, and

54
00:03:45,000 --> 00:03:49,990
attribute references, and then finally, left to right the Binding operator,

55
00:03:50,000 --> 00:03:54,990
which is the parentheses when it's not meant to be a tuple and then tuple,

56
00:03:55,000 --> 00:03:56,990
lists and dictionaries.

57
00:03:57,000 --> 00:03:58,990
So this is operator precedence in Python.

58
00:03:59,000 --> 00:04:04,990
Again, it's much better for you to not rely on this, to use parentheses to

59
00:04:05,000 --> 00:04:08,990
say exactly what it is that you mean to say when you are writing complex expressions.

60
00:04:09,000 --> 00:04:12,990
When you are reading other people's code that have not been that kind to you,

61
00:04:13,000 --> 00:04:16,990
it's good to know that this table exists and where to find it, so that you can

62
00:04:17,000 --> 00:04:27,000
read that code and know exactly how it gets evaluated.

