1 00:00:00,780 --> 00:00:03,060 Instructor: All right, let's play a little game. 2 00:00:03,060 --> 00:00:05,250 Can you guess what the output of this program 3 00:00:05,250 --> 00:00:06,810 is going to be? 4 00:00:06,810 --> 00:00:08,880 I have over here, a = 1. 5 00:00:08,880 --> 00:00:12,540 Then we create a function called confusion 6 00:00:12,540 --> 00:00:14,520 where we have a = 5 7 00:00:14,520 --> 00:00:16,350 and we return a. 8 00:00:16,350 --> 00:00:17,910 My question is, 9 00:00:17,910 --> 00:00:21,720 when we print a and when we print confusion, 10 00:00:21,720 --> 00:00:24,090 what do you expect to happen here? 11 00:00:24,090 --> 00:00:25,530 Take a second to figure out. 12 00:00:25,530 --> 00:00:27,150 Pause if you need to. 13 00:00:27,150 --> 00:00:28,100 And let's find out. 14 00:00:31,080 --> 00:00:33,030 1 and 5. 15 00:00:33,030 --> 00:00:35,160 Is that what you expected? 16 00:00:35,160 --> 00:00:36,260 Let's go line by line. 17 00:00:37,230 --> 00:00:42,230 First, we assign value of 1 to variable a. 18 00:00:43,080 --> 00:00:46,740 We then create a function called confusion. 19 00:00:46,740 --> 00:00:48,540 And python interpreter is going to say, 20 00:00:48,540 --> 00:00:50,040 all right, confusion function. 21 00:00:50,040 --> 00:00:50,873 I know what it is. 22 00:00:50,873 --> 00:00:52,020 I'm gonna put it in memory. 23 00:00:52,020 --> 00:00:54,330 When you need to use me, just let me know. 24 00:00:54,330 --> 00:00:56,850 And then it's going to go to line 8. 25 00:00:56,850 --> 00:00:58,170 And line 8 is going to say, 26 00:00:58,170 --> 00:00:59,910 hey, print a. 27 00:00:59,910 --> 00:01:01,170 What's a? 28 00:01:01,170 --> 00:01:04,739 Well, the only time that I've seen a is right here, 29 00:01:04,739 --> 00:01:05,910 a = 1. 30 00:01:05,910 --> 00:01:07,860 So going to memory, what's a? 31 00:01:07,860 --> 00:01:09,330 a = 1. 32 00:01:09,330 --> 00:01:12,663 But then when I run confusion like this, 33 00:01:13,530 --> 00:01:14,363 I'm going to say, 34 00:01:14,363 --> 00:01:17,970 hey, a is going to equal to 5. 35 00:01:17,970 --> 00:01:21,300 And now we return a, which is 5. 36 00:01:21,300 --> 00:01:22,133 Okay. 37 00:01:22,133 --> 00:01:24,840 What if I change the order of this? 38 00:01:24,840 --> 00:01:26,340 What if I go like this? 39 00:01:26,340 --> 00:01:27,990 What will happen? 40 00:01:27,990 --> 00:01:29,043 if I click run, 41 00:01:31,830 --> 00:01:33,303 is that what you expected? 42 00:01:36,300 --> 00:01:38,970 Once again, we have a = 1, 43 00:01:38,970 --> 00:01:40,980 then we define the function, 44 00:01:40,980 --> 00:01:42,993 and then we print confusion. 45 00:01:44,670 --> 00:01:49,670 Now, confusion here is going to assign a equal to 5 46 00:01:49,860 --> 00:01:52,650 and then return a. 47 00:01:52,650 --> 00:01:55,620 But why didn't this change? 48 00:01:55,620 --> 00:01:59,760 Why is a still equal to 1 by the time we get here? 49 00:01:59,760 --> 00:02:03,240 And this is because of scope, right? 50 00:02:03,240 --> 00:02:06,360 What the Python interpreter does 51 00:02:06,360 --> 00:02:09,990 is when it runs the confusion function, 52 00:02:09,990 --> 00:02:10,823 it's going to say, 53 00:02:10,823 --> 00:02:13,650 hey, a is going to equal to 5 54 00:02:13,650 --> 00:02:17,190 but I'm creating a variable here called a 55 00:02:17,190 --> 00:02:19,200 in my own universe. 56 00:02:19,200 --> 00:02:21,960 I don't know anything about a = 1 here. 57 00:02:21,960 --> 00:02:24,390 I have my own universe. 58 00:02:24,390 --> 00:02:26,430 And after we run this function, 59 00:02:26,430 --> 00:02:27,750 then, well, we're done. 60 00:02:27,750 --> 00:02:30,000 We've returned the function and it's completely gone. 61 00:02:30,000 --> 00:02:33,753 But we never left the universe into the global scope. 62 00:02:35,490 --> 00:02:37,530 So there's a set of rules 63 00:02:37,530 --> 00:02:40,350 that the Python interpreter goes through 64 00:02:40,350 --> 00:02:42,120 to check a variable. 65 00:02:42,120 --> 00:02:44,190 And the rules are this. 66 00:02:44,190 --> 00:02:47,820 First, it's going to check with the local. 67 00:02:47,820 --> 00:02:50,940 So start with local. 68 00:02:50,940 --> 00:02:53,070 And what we call is a local scope. 69 00:02:53,070 --> 00:02:57,450 A local scope is a scope that's part of this local function. 70 00:02:57,450 --> 00:02:59,790 So a is local scope. 71 00:02:59,790 --> 00:03:00,720 It's going to check there. 72 00:03:00,720 --> 00:03:03,690 Hey, do I know the variable a? 73 00:03:03,690 --> 00:03:04,620 Well, yeah. 74 00:03:04,620 --> 00:03:06,030 When I return a, 75 00:03:06,030 --> 00:03:09,000 I have a variable defined here. 76 00:03:09,000 --> 00:03:11,130 So it starts there. 77 00:03:11,130 --> 00:03:13,323 But let's say a wasn't there. 78 00:03:14,190 --> 00:03:15,640 Let's say I remove a 79 00:03:16,680 --> 00:03:17,973 and I click run here. 80 00:03:19,590 --> 00:03:20,820 I get both ones. 81 00:03:20,820 --> 00:03:22,920 Because the second rule is 82 00:03:22,920 --> 00:03:27,450 if there's nothing in the local variable or local scope, 83 00:03:27,450 --> 00:03:29,040 then number two, 84 00:03:29,040 --> 00:03:31,683 is there a parent local scope? 85 00:03:33,180 --> 00:03:36,750 In our case, what's the parent of this function? 86 00:03:36,750 --> 00:03:39,030 Well, it's the global scope. 87 00:03:39,030 --> 00:03:42,480 But we can also do another function. 88 00:03:42,480 --> 00:03:44,970 Let's say, parent. 89 00:03:44,970 --> 00:03:49,970 And parent is a parent of the confusion function. 90 00:03:50,070 --> 00:03:53,373 And in here, let's say a = 10. 91 00:03:54,810 --> 00:03:59,010 Now in here, we will return confusion 92 00:03:59,010 --> 00:04:01,200 just so we can use it again 93 00:04:01,200 --> 00:04:02,613 and actually run it. 94 00:04:03,510 --> 00:04:05,110 So let's call parent 95 00:04:07,980 --> 00:04:09,603 and click run. 96 00:04:11,520 --> 00:04:14,520 All right, let's go through this so it's not too confusing. 97 00:04:14,520 --> 00:04:17,550 We have the parent function that gets called. 98 00:04:17,550 --> 00:04:18,420 So when we call that, 99 00:04:18,420 --> 00:04:20,850 we assign a equals to 10, 100 00:04:20,850 --> 00:04:24,150 and then we also create a new function confusion. 101 00:04:24,150 --> 00:04:28,680 And then finally, we return confusion and actually run it. 102 00:04:28,680 --> 00:04:31,680 So we're going to return a, 103 00:04:31,680 --> 00:04:32,760 and then in here, 104 00:04:32,760 --> 00:04:34,950 print whatever the value of a is, 105 00:04:34,950 --> 00:04:36,240 which is 10. 106 00:04:36,240 --> 00:04:37,530 And why is that? 107 00:04:37,530 --> 00:04:40,170 Well, because we first start with the local scope. 108 00:04:40,170 --> 00:04:43,020 Hey, does confusion know what a is? 109 00:04:43,020 --> 00:04:45,210 Nope, I don't have it in my local scope. 110 00:04:45,210 --> 00:04:47,700 Okay, Does my parent local scope have it? 111 00:04:47,700 --> 00:04:49,830 So it's going to go up a level and say, 112 00:04:49,830 --> 00:04:52,410 hey, does parent function know what a is? 113 00:04:52,410 --> 00:04:54,180 Yep, I have a = 10. 114 00:04:54,180 --> 00:04:55,860 So it's going to print that. 115 00:04:55,860 --> 00:05:00,573 The third rule or the third order is global. 116 00:05:02,220 --> 00:05:06,480 And global is what we call the indentation of nothing. 117 00:05:06,480 --> 00:05:07,620 It's this file, right? 118 00:05:07,620 --> 00:05:11,490 Whatever the file has, that is global. 119 00:05:11,490 --> 00:05:14,850 In our case, if a = 10 doesn't exist here, 120 00:05:14,850 --> 00:05:16,950 there's no local, there's no parent local, 121 00:05:16,950 --> 00:05:17,973 and I click run, 122 00:05:20,010 --> 00:05:22,323 it checks the global scope. 123 00:05:24,930 --> 00:05:27,090 And then finally, there's one other rule, 124 00:05:27,090 --> 00:05:29,760 and this is number four, and it's a tricky one. 125 00:05:29,760 --> 00:05:33,460 And this is what we call built-in Python 126 00:05:35,370 --> 00:05:38,223 or built-in Python functions. 127 00:05:39,360 --> 00:05:43,350 So Python comes with predefined functions 128 00:05:43,350 --> 00:05:47,343 such as, well, the sum that we've talked about before. 129 00:05:48,510 --> 00:05:51,423 This sum, let's say just give it 5, 130 00:05:52,530 --> 00:05:54,630 is going to check, 131 00:05:54,630 --> 00:05:56,100 or actually let's not give it anything, 132 00:05:56,100 --> 00:05:57,393 let's just do sum here. 133 00:05:59,760 --> 00:06:01,353 When I run this, 134 00:06:03,330 --> 00:06:05,733 I get built-in function sum. 135 00:06:07,170 --> 00:06:10,890 Now, how did I know what this was? 136 00:06:10,890 --> 00:06:13,920 What this variable at the end of the day was? 137 00:06:13,920 --> 00:06:15,090 Well, because it said, 138 00:06:15,090 --> 00:06:19,410 hey, do I have sum inside of this universe? 139 00:06:19,410 --> 00:06:21,210 Nope, I don't. 140 00:06:21,210 --> 00:06:24,603 Okay, do I have some in my parent scope? 141 00:06:25,710 --> 00:06:27,450 Nope, I don't. 142 00:06:27,450 --> 00:06:30,030 Okay, do I have some in global? 143 00:06:30,030 --> 00:06:32,310 Nope, there's nothing sum in here. 144 00:06:32,310 --> 00:06:34,650 And then the final thing it's going to check is, 145 00:06:34,650 --> 00:06:39,000 hey, is sum some sort of a built-in Python function? 146 00:06:39,000 --> 00:06:40,830 Yep, Python actually gives us sum, 147 00:06:40,830 --> 00:06:41,670 so you can use it. 148 00:06:41,670 --> 00:06:43,233 So it doesn't error out. 149 00:06:44,220 --> 00:06:46,410 And this is the scope rules 150 00:06:46,410 --> 00:06:49,200 that the Python interpreter falls. 151 00:06:49,200 --> 00:06:50,820 Very, very cool. 152 00:06:50,820 --> 00:06:53,250 There's still a few more things that we need to learn. 153 00:06:53,250 --> 00:06:54,950 So I'll see you in the next video.