1 00:00:00,570 --> 00:00:02,100 Instructor: Welcome back. 2 00:00:02,100 --> 00:00:04,950 Let's talk about a important term 3 00:00:04,950 --> 00:00:06,180 when it comes to programming, 4 00:00:06,180 --> 00:00:09,453 and that is operator precedence. 5 00:00:11,520 --> 00:00:13,170 Now, what does that mean? 6 00:00:13,170 --> 00:00:17,550 Again, we're using our knowledge from math class. 7 00:00:17,550 --> 00:00:19,710 It's the idea that if I do 8 00:00:19,710 --> 00:00:21,030 20 9 00:00:21,030 --> 00:00:21,863 plus 10 00:00:21,863 --> 00:00:22,740 3 11 00:00:22,740 --> 00:00:24,150 times 12 00:00:24,150 --> 00:00:25,530 4, 13 00:00:25,530 --> 00:00:27,540 it means that different operators 14 00:00:27,540 --> 00:00:30,570 have precedents over different ones. 15 00:00:30,570 --> 00:00:35,160 That is, we multiply this first, equals to 12, 16 00:00:35,160 --> 00:00:37,743 and then 20 gets added to 12, 17 00:00:38,910 --> 00:00:41,580 because the Python interpreter is going 18 00:00:41,580 --> 00:00:44,013 to follow a basic set of rules. 19 00:00:44,970 --> 00:00:48,363 For example, if I do 20 minus 3 here, 20 00:00:49,740 --> 00:00:51,483 and let's print this out, 21 00:00:53,430 --> 00:00:54,273 click run, 22 00:00:56,430 --> 00:00:58,110 I get 8. 23 00:00:58,110 --> 00:01:02,040 because 3 times 4 gets evaluated first, 24 00:01:02,040 --> 00:01:06,210 which equals to 12, and then subtract it from 20. 25 00:01:06,210 --> 00:01:08,970 It's not that this goes first 26 00:01:08,970 --> 00:01:11,283 and then we multiply 17 by 4, 27 00:01:12,120 --> 00:01:15,180 and this operator precedence shouldn't be new, right? 28 00:01:15,180 --> 00:01:17,610 If you've taken any sort of math class 29 00:01:17,610 --> 00:01:21,630 you understand that this is how, well, math works, 30 00:01:21,630 --> 00:01:24,720 and this works the same in all programming languages. 31 00:01:24,720 --> 00:01:27,660 So the order is this: 32 00:01:27,660 --> 00:01:30,210 First, we follow the brackets. 33 00:01:30,210 --> 00:01:33,570 So, whatever is wrapped in a bracket, we do first. 34 00:01:33,570 --> 00:01:38,160 The second is we do the power of. 35 00:01:38,160 --> 00:01:41,160 So whatever is done in the power of 36 00:01:41,160 --> 00:01:43,530 will be after parentheses. 37 00:01:43,530 --> 00:01:47,410 For example, if I do parentheses here 38 00:01:48,990 --> 00:01:53,550 and then to the power of, let's say, 2 39 00:01:53,550 --> 00:01:57,510 or let's do plus 2 to the power of 2, 40 00:01:57,510 --> 00:02:02,510 well the first operation that will happen is 20 minus 3 41 00:02:02,760 --> 00:02:06,060 because that's the first precedence, 42 00:02:06,060 --> 00:02:08,250 and then we do 2 to the power of 2, 43 00:02:08,250 --> 00:02:10,502 because that's the next precedence, 44 00:02:12,360 --> 00:02:16,110 and then only after we do that, we add these two together. 45 00:02:16,110 --> 00:02:17,253 If I click run, 46 00:02:18,630 --> 00:02:20,190 I get 21. 47 00:02:20,190 --> 00:02:24,063 Which again makes sense, 17 plus 4. 48 00:02:26,220 --> 00:02:31,220 After the power of we have the multiplication and division. 49 00:02:33,660 --> 00:02:36,330 And then finally the last precedence 50 00:02:36,330 --> 00:02:38,853 is addition and subtraction. 51 00:02:39,990 --> 00:02:42,630 Now, this isn't going to be a math class. 52 00:02:42,630 --> 00:02:46,680 Some people really love math, some people really don't. 53 00:02:46,680 --> 00:02:50,400 But I want to just give you this basic understanding 54 00:02:50,400 --> 00:02:52,560 of operator precedence when you see it. 55 00:02:52,560 --> 00:02:54,150 We'll do some exercises here 56 00:02:54,150 --> 00:02:56,700 to get accustomed to it, but trust me, 57 00:02:56,700 --> 00:02:59,850 this is as mathy as we're gonna get in this course. 58 00:02:59,850 --> 00:03:03,360 The math part of the course is, well, just here. 59 00:03:03,360 --> 00:03:05,400 It's gonna get a lot more interesting 60 00:03:05,400 --> 00:03:08,010 as we learn about different data types. 61 00:03:08,010 --> 00:03:09,540 I'll see you in the next one. 62 00:03:09,540 --> 00:03:10,373 Bye bye.