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