1 00:00:00,290 --> 00:00:06,330 Welcome back. Now, for this video I want to be a little bit more precise about how 2 00:00:06,330 --> 00:00:08,790 Python evaluates expressions in 3 00:00:08,790 --> 00:00:12,375 the order that Python evaluates different expression partson. 4 00:00:12,375 --> 00:00:14,490 Now, when we're a doing a function call, 5 00:00:14,490 --> 00:00:19,665 the first thing that Python does is it evaluates the function that we're calling itself. 6 00:00:19,665 --> 00:00:28,199 So, it evaluates the function itself. 7 00:00:28,199 --> 00:00:30,060 So, in other words, 8 00:00:30,060 --> 00:00:33,030 let's suppose that we're calling this add function. 9 00:00:33,030 --> 00:00:35,010 The first thing that Python is going to do, 10 00:00:35,010 --> 00:00:38,145 is it's going to look up what is the value of 11 00:00:38,145 --> 00:00:42,435 this expression and it's going to hopefully see that ad as a function. 12 00:00:42,435 --> 00:00:46,655 Now the function call expression might be something more complicated 13 00:00:46,655 --> 00:00:51,470 than just a variable name as long as it evaluates to a function. 14 00:00:51,470 --> 00:00:54,440 Then the next thing that Python is going to do, 15 00:00:54,440 --> 00:00:59,415 is it's going to evaluate each one of the arguments in order. 16 00:00:59,415 --> 00:01:08,850 So, it evaluates the arguments from left to right. 17 00:01:10,160 --> 00:01:12,445 So, in this example, 18 00:01:12,445 --> 00:01:14,300 the first argument is square. 19 00:01:14,300 --> 00:01:17,795 So, Python is going to compute the value of 20 00:01:17,795 --> 00:01:21,470 this expression and then it's going to compute the value of 21 00:01:21,470 --> 00:01:25,520 this expression and then finally after Python knows 22 00:01:25,520 --> 00:01:30,170 what function it's calling and what the value of every argument is, 23 00:01:30,170 --> 00:01:32,570 then Python is going to finally call 24 00:01:32,570 --> 00:01:39,270 the function and then 25 00:01:39,270 --> 00:01:41,855 the value that that function call returns 26 00:01:41,855 --> 00:01:45,260 is going to be the value of this overall expression. 27 00:01:45,260 --> 00:01:48,145 So, let's see that in action. 28 00:01:48,145 --> 00:01:53,300 So, here we assign X to five and Y to seven and the first thing that Python does, 29 00:01:53,300 --> 00:01:55,940 is it evaluates this add function. 30 00:01:55,940 --> 00:02:00,320 So, it sees that adds a function and so the next thing that it does, 31 00:02:00,320 --> 00:02:03,190 is it evaluates each one of its arguments. 32 00:02:03,190 --> 00:02:09,200 So, first, it evaluates this sub-expression and it sees that square itself 33 00:02:09,200 --> 00:02:11,450 is a function and so it's going to repeat 34 00:02:11,450 --> 00:02:15,820 that process and it looks up square and finds that it's a function. 35 00:02:15,820 --> 00:02:18,535 Then it repeats that process, looks up Y, 36 00:02:18,535 --> 00:02:26,080 finds that its value is seven and then it's going to call square on seven and get 49. 37 00:02:26,080 --> 00:02:28,895 Then it's going to look up the value of 38 00:02:28,895 --> 00:02:31,100 this square function and it's going to see that it's 39 00:02:31,100 --> 00:02:36,050 a function it's going to compute the value of each one of its arguments. 40 00:02:36,050 --> 00:02:40,445 So, it's going to see that X is five and then it's going to 41 00:02:40,445 --> 00:02:46,195 call square on five to get 25. 42 00:02:46,195 --> 00:02:49,445 So, far what Python did, 43 00:02:49,445 --> 00:02:55,190 was it found what's the value of ad and then it computed the value of 44 00:02:55,190 --> 00:02:59,690 its two arguments and then it ended up getting 49 as 45 00:02:59,690 --> 00:03:01,940 the first argument and 25 as 46 00:03:01,940 --> 00:03:05,570 the second argument and so now that we have the function itself, 47 00:03:05,570 --> 00:03:08,645 and all of the arguments then we can finally call the function 48 00:03:08,645 --> 00:03:13,775 add on 49 and 25 and as a result, 49 00:03:13,775 --> 00:03:20,190 we're going to get 74 as the value of this overall expression. 50 00:03:20,450 --> 00:03:28,940 Now, suppose that we have this complex expression square of X plus sub square of 51 00:03:28,940 --> 00:03:32,660 Y and two times X and we're asked to 52 00:03:32,660 --> 00:03:37,670 figure out what order is Python actually going to evaluate these arguments in. 53 00:03:37,670 --> 00:03:43,960 Now, bear with me for a bit as I drag each one of these blocks in the correct order. 54 00:03:43,960 --> 00:03:48,080 Okay. So, the first thing that Python is going to do, 55 00:03:48,080 --> 00:03:55,655 is it's going to try to look up the square function and so if we see yep 56 00:03:55,655 --> 00:03:59,690 lookup the variable square to get the function object and then it 57 00:03:59,690 --> 00:04:03,905 needs to compute the value of each one of its arguments. 58 00:04:03,905 --> 00:04:08,870 So, here there's only one argument and so the first thing that Python is 59 00:04:08,870 --> 00:04:14,095 going to do when figuring out what's the value of X plus sub of this function, 60 00:04:14,095 --> 00:04:18,260 is it's first going to look up the value of X because that's leftmost. 61 00:04:18,260 --> 00:04:22,355 So, it's going to be look up the value of variable X. 62 00:04:22,355 --> 00:04:28,090 So, I'm going to get lookup the value of variable X to get two. 63 00:04:28,090 --> 00:04:32,065 Then before we can add X plus this expression, 64 00:04:32,065 --> 00:04:35,105 then we need to compute the value of this expression. 65 00:04:35,105 --> 00:04:38,060 So, when computing the value of this expression, 66 00:04:38,060 --> 00:04:42,690 then we need to in turn lookup the value of the sub-function. 67 00:04:42,690 --> 00:04:47,910 So, we need to look up the variable sub to get 68 00:04:47,910 --> 00:04:54,185 that function object and then we're computing the value of its arguments. 69 00:04:54,185 --> 00:04:56,735 So, the first argument is square of Y. 70 00:04:56,735 --> 00:04:58,180 So, in order to get this, 71 00:04:58,180 --> 00:05:02,705 we need to look up what's the square function. 72 00:05:02,705 --> 00:05:06,440 So, we look up the variable square again to get 73 00:05:06,440 --> 00:05:09,680 the function object and then we need 74 00:05:09,680 --> 00:05:14,075 to compute the value of its argument so we need to look up the value of Y. 75 00:05:14,075 --> 00:05:19,580 So, we'd look up the value of Y to get three and then we back 76 00:05:19,580 --> 00:05:23,015 up and we need to compute the value 77 00:05:23,015 --> 00:05:26,615 of the second argument to sub and in order to do that, 78 00:05:26,615 --> 00:05:35,890 we need to look up X again to get two and we need to multiply two-by-two to get four. 79 00:05:37,430 --> 00:05:43,355 So, now we have the value of both of these expressions. 80 00:05:43,355 --> 00:05:45,995 Oh, excuse me. There's one more thing here. 81 00:05:45,995 --> 00:05:49,990 So, after we look up the value of variable Y to get three, 82 00:05:49,990 --> 00:05:52,805 we actually call the square function. 83 00:05:52,805 --> 00:05:55,835 So, here after we look up Y to get three, 84 00:05:55,835 --> 00:05:57,980 we have to run the square function. 85 00:05:57,980 --> 00:06:02,330 Okay. So, now we have the value of this sub-expression or 86 00:06:02,330 --> 00:06:08,359 this argument and this argument and so now we can finally call this sub function. 87 00:06:08,359 --> 00:06:15,450 So, we run the sub-function on inputs four and nine giving us the value five. 88 00:06:15,450 --> 00:06:18,630 So, then we get X plus five. 89 00:06:18,630 --> 00:06:23,030 So, that's going to give us two plus five and we get 90 00:06:23,030 --> 00:06:27,530 seven and that means that we're calling square with seven as 91 00:06:27,530 --> 00:06:32,030 an argument and so we run the square function 92 00:06:32,030 --> 00:06:37,860 again on the input seven giving us the final value of 49. 93 00:06:38,330 --> 00:06:42,190 That's all for now until next time.