1 00:00:00,360 --> 00:00:07,290 In order to finalize our understanding of functions, we have to talk about scope, another one of our 2 00:00:07,290 --> 00:00:07,950 key terms. 3 00:00:08,250 --> 00:00:09,420 What is scope? 4 00:00:13,730 --> 00:00:24,080 Scope is something present in a lot of programming languages and scope simply means what variables do 5 00:00:24,080 --> 00:00:26,300 I have access to? 6 00:00:28,850 --> 00:00:30,140 So what do I mean by that? 7 00:00:31,260 --> 00:00:39,120 Well, we've seen this before, right, where I do print and then let's say, Nate, if I click run. 8 00:00:41,050 --> 00:00:48,280 I'll get an air, a name, air name, name is not defined because, well, it doesn't exist, right? 9 00:00:48,460 --> 00:00:52,900 And up to this point, we haven't really discussed this, but this is because of scope. 10 00:00:53,980 --> 00:00:59,860 What variables do I have access to the Python interpreter says, hey, what do I have access to? 11 00:01:00,040 --> 00:01:05,530 And when we use something that it doesn't understand or doesn't have access to, it's going to throw 12 00:01:05,530 --> 00:01:06,160 a nightmare. 13 00:01:07,140 --> 00:01:16,020 Or variable is not defined, air and scope in Python has what we call functional scope or function scope. 14 00:01:17,260 --> 00:01:17,950 What does that mean? 15 00:01:18,520 --> 00:01:27,610 Well, up until now, when we create a variable like total equals to a hundred, this is part of what 16 00:01:27,610 --> 00:01:28,970 we call global scope. 17 00:01:29,380 --> 00:01:35,010 That means anybody on this file has access to this total variable. 18 00:01:35,050 --> 00:01:37,300 I can use this inside of a conditional block. 19 00:01:37,300 --> 00:01:39,430 I can use it inside of a function. 20 00:01:39,700 --> 00:01:41,230 It has global scope. 21 00:01:41,830 --> 00:01:44,560 But when I say Python has function scope. 22 00:01:45,730 --> 00:01:54,730 What I mean is that when we create a function and let's say we create total as some or let's say some 23 00:01:55,270 --> 00:01:55,750 func. 24 00:01:57,760 --> 00:02:06,130 And we create total inside of the function, if I print total here and I click run. 25 00:02:07,760 --> 00:02:15,220 I get the same thing, name total is not defined, this is because of Python's function scope, any 26 00:02:15,230 --> 00:02:19,100 time we create a variable, if it's not inside of a function. 27 00:02:20,140 --> 00:02:25,930 Then it is part of the global scope, we have access to it, but you see over here we get that red underlined, 28 00:02:26,230 --> 00:02:27,760 undefined name total. 29 00:02:28,240 --> 00:02:32,080 If let's say, for example, I had a condition if true. 30 00:02:34,250 --> 00:02:42,350 Then X equals to 10 if I do this and I say X and I click run. 31 00:02:44,520 --> 00:02:49,080 I have 10 because, well, although I have indentation. 32 00:02:50,280 --> 00:02:55,770 The only time I create a new scope, you can think of this as a new universe that we spawn. 33 00:02:56,700 --> 00:03:04,680 Well, we can only do that when we define a function like this, so think of scope as a new world that 34 00:03:04,680 --> 00:03:10,380 we create in our case, when we create a function, we create a new world that anything that's indented 35 00:03:10,380 --> 00:03:14,970 inside of the function is its own world that we don't really have access to. 36 00:03:15,000 --> 00:03:18,690 We can only use total if we indent print. 37 00:03:20,430 --> 00:03:21,780 And it's part of this world. 38 00:03:23,230 --> 00:03:26,930 That's what scope is, who has access to who. 39 00:03:27,760 --> 00:03:33,040 So let's take a break here and I want to play a little game in the next video where we try and figure 40 00:03:33,040 --> 00:03:35,410 out who has access to who. 41 00:03:35,980 --> 00:03:37,440 I'll see in that one by.