1 00:00:00,830 --> 00:00:01,290 All right. 2 00:00:01,340 --> 00:00:02,940 Let's play a little game. 3 00:00:03,110 --> 00:00:08,900 Can you guess what the output of this program is going to be I have over here a close one. 4 00:00:08,940 --> 00:00:15,350 Then we create a function called confusion where we have a equals to five and we return. 5 00:00:15,360 --> 00:00:23,390 Hey my question is when we print a and when we print confusion What do you expect to happen here. 6 00:00:24,140 --> 00:00:25,490 Take a second to figure out. 7 00:00:25,520 --> 00:00:27,850 Pause if you need to and let's find out 8 00:00:31,120 --> 00:00:33,010 one in five. 9 00:00:33,120 --> 00:00:35,210 Is that what you expected. 10 00:00:35,220 --> 00:00:37,060 Let's go line by line. 11 00:00:37,280 --> 00:00:42,810 First we assign a value of 1 to variable a. 12 00:00:43,130 --> 00:00:49,070 We then create a function called confusion and Python interpreter is going to say all right. 13 00:00:49,070 --> 00:00:50,050 Confusion function. 14 00:00:50,060 --> 00:00:50,800 I know what it is. 15 00:00:50,810 --> 00:00:53,290 I'm going to put it in memory when you need to use me. 16 00:00:53,300 --> 00:00:54,280 Just let me know. 17 00:00:54,470 --> 00:01:01,220 And then it's going to go to line eight and line eight is going to say hey print a what's a. 18 00:01:01,300 --> 00:01:05,950 Well the only time that I've seen a is right here A equals one. 19 00:01:05,960 --> 00:01:09,280 So go into memory what's a equals one. 20 00:01:09,320 --> 00:01:18,720 But then when I run confusion like this I'm going to say hey a is going to equal to five and now we 21 00:01:18,720 --> 00:01:21,010 return eight which is five. 22 00:01:21,400 --> 00:01:24,880 OK what if I change the order of this. 23 00:01:24,900 --> 00:01:26,370 What if I go like this. 24 00:01:26,370 --> 00:01:31,900 What will happen if I click Run. 25 00:01:31,990 --> 00:01:33,120 Is that what you expected 26 00:01:36,330 --> 00:01:37,240 once again. 27 00:01:37,380 --> 00:01:38,910 We have a close one. 28 00:01:39,000 --> 00:01:45,060 Then we define the function and then we print confusion now. 29 00:01:45,100 --> 00:01:55,680 Confusion here is going to assign a equal to five and then return a but why didn't this change. 30 00:01:55,680 --> 00:01:58,920 Why is a still equal to 1 by the time we get here. 31 00:01:59,850 --> 00:02:01,890 And this is because of scope. 32 00:02:01,980 --> 00:02:11,100 Right what the Python interpreter does is when it runs the confusion function it's going to say hey 33 00:02:11,640 --> 00:02:19,290 a is going to equal to 5 but I'm creating a variable here called a in my own universe. 34 00:02:19,290 --> 00:02:21,990 I don't know anything about a equals one here. 35 00:02:22,020 --> 00:02:24,280 I have my own universe. 36 00:02:24,480 --> 00:02:29,670 And after we run this function then while we're done we've returned the function and it's completely 37 00:02:29,670 --> 00:02:38,950 gone but we never left the universe into the global scope so there's a set of rules that the Python 38 00:02:38,950 --> 00:02:42,220 interpreter goes through to check a variable. 39 00:02:42,220 --> 00:02:47,890 And the rules are this first it's going to check with the local. 40 00:02:47,890 --> 00:02:54,810 So start with local and what we call is a local scope a local scope is a scope. 41 00:02:54,820 --> 00:02:57,520 That's part of this local function. 42 00:02:57,520 --> 00:02:59,830 So a is a local scope. 43 00:02:59,860 --> 00:03:00,730 It's going to check there. 44 00:03:00,730 --> 00:03:04,050 Hey do I know the variable A BOAT. 45 00:03:04,090 --> 00:03:04,690 YEAH. 46 00:03:04,690 --> 00:03:09,040 WHEN I RETURN A I have a variable defined here. 47 00:03:09,040 --> 00:03:11,110 So it starts there. 48 00:03:11,110 --> 00:03:14,150 But let's say a wasn't there. 49 00:03:14,230 --> 00:03:24,240 Let's say I remove a and I click Run here I get both once because the second rule is if there is nothing 50 00:03:24,240 --> 00:03:33,880 in the local variable or local scope then number two is there a parent local scope in our case. 51 00:03:33,910 --> 00:03:36,760 What's the parent of this function. 52 00:03:36,790 --> 00:03:47,410 Well it's the global scope but we can also do another function let's say parent and parent is a parent 53 00:03:47,620 --> 00:03:50,090 of the confusion function. 54 00:03:50,090 --> 00:03:59,020 And in here let's say a equals to 10 now in here we'll return confusion. 55 00:03:59,030 --> 00:04:02,500 Just so we can use it again and actually run it. 56 00:04:03,560 --> 00:04:05,000 So let's call parent 57 00:04:08,040 --> 00:04:11,600 and click Run. 58 00:04:11,600 --> 00:04:12,630 All right let's go through this. 59 00:04:12,650 --> 00:04:14,570 So it's not too confusing. 60 00:04:14,570 --> 00:04:17,620 We have their parent function that gets called. 61 00:04:17,620 --> 00:04:24,170 So when we call that we assign a equals to 10 and then we also create a new function confusion. 62 00:04:24,230 --> 00:04:28,690 And then finally we return confusion and actually run it. 63 00:04:28,730 --> 00:04:31,570 So we're going to return a. 64 00:04:31,730 --> 00:04:37,570 And then in here print whatever the value of a is which is 10 and why is that. 65 00:04:37,580 --> 00:04:40,340 Well because we first start with the local scope. 66 00:04:40,340 --> 00:04:43,070 Hey does confusion know what a is. 67 00:04:43,070 --> 00:04:45,290 No I don't have it in my local scope. 68 00:04:45,290 --> 00:04:45,740 OK. 69 00:04:45,770 --> 00:04:47,740 Does my parent local scope have it. 70 00:04:47,750 --> 00:04:52,450 So it's going to go up a level and say Hey does parent function know what it is. 71 00:04:52,460 --> 00:04:52,750 Yep. 72 00:04:52,850 --> 00:04:54,230 I have a equals to 10. 73 00:04:54,260 --> 00:04:55,760 So it's going to print that. 74 00:04:55,910 --> 00:05:06,540 The third rule or the Third Order is global and global is what we call the indentation of nothing. 75 00:05:06,540 --> 00:05:07,400 It's this file. 76 00:05:07,410 --> 00:05:07,630 Right. 77 00:05:07,630 --> 00:05:11,560 Whatever the File has that is global. 78 00:05:11,560 --> 00:05:17,470 In our case if a equals to 10 doesn't exist here there's no local there's no parent local and I click 79 00:05:17,470 --> 00:05:22,150 Run it checks the global scope 80 00:05:25,020 --> 00:05:29,820 and then finally there's one other rule and this is number four and it's a tricky one. 81 00:05:29,820 --> 00:05:42,610 And this is what we call built in Python or built in Python functions so Python comes with predefined 82 00:05:42,620 --> 00:05:51,290 functions such as well the sum that we've talked about before this some let's say just give it five 83 00:05:52,580 --> 00:06:03,800 is going to check or actually let's not give it anything let's just do some here when I run this I get 84 00:06:03,830 --> 00:06:06,950 built in function some. 85 00:06:07,220 --> 00:06:10,940 Now how do you know what this was. 86 00:06:10,940 --> 00:06:13,900 What this variable at the end of the day was. 87 00:06:13,970 --> 00:06:19,420 Well it because it said hey do I have some inside of this universe. 88 00:06:19,430 --> 00:06:21,260 No I don't. 89 00:06:21,260 --> 00:06:21,750 OK. 90 00:06:21,830 --> 00:06:25,670 Do I have some in my parent scope. 91 00:06:25,730 --> 00:06:26,800 No I don't. 92 00:06:27,500 --> 00:06:27,710 OK. 93 00:06:27,710 --> 00:06:30,060 Do I have a sum in global. 94 00:06:30,080 --> 00:06:30,530 No. 95 00:06:30,600 --> 00:06:36,800 There's nothing some in here and then the final thing it's going to check is hey is some some sort of 96 00:06:36,800 --> 00:06:39,050 a built in python function. 97 00:06:39,050 --> 00:06:39,420 Yep. 98 00:06:39,530 --> 00:06:40,860 Python actually gives us some. 99 00:06:40,880 --> 00:06:49,130 So you can use it so it doesn't error out and this is the scope rules that the Python interpreter falls. 100 00:06:49,250 --> 00:06:50,850 Very very cool. 101 00:06:50,870 --> 00:06:53,270 There's still a few more things that we need to learn. 102 00:06:53,350 --> 00:06:54,670 So I'll see you in the next video.