 Yeah, hi everybody in this video I just want to give you a quick idea of what global versus local means somebody asked on the channel So I thought I'd make a quick video. Sorry about the sound somebody borrowed my mic So you'll probably hear a little more keyboarding than usual. Anyway, so let's get started So here I am in my program and I want to make a variable x and I want to set that equal to five And I'm gonna go down a little bit. I'm gonna say print x run that I am using an editor called genie and This is the output is five. Okay, so this is what's known as the global space I am here in the main program and I have defined this variable here in the main program Now, let me go ahead and change this and start using a function. So I'll make a new function and Just call it print value and In this case, I'm gonna put print x And then to use a function I need to call it So print value So I'm gonna run that and see what we get Okay, I still get a five here. Okay now So I've defined this x out here in the global space I'm printing it here in what's known as a local space So this is local meaning only local to this particular function now This particular function doesn't have any reference to x except for this print So if I do something like this x equals seven And I run it You see how it now prints seven So here's a question for you because I changed x to seven here If I print x now outside the function, what's the value going to be? Probably depends on the programming You got two choices either it's gonna be five or it's gonna be seven So if you think it's five raise your hand things to be seven don't raise your hand. So let's test it and It is five. Okay, and this is something that often confuses beginning programmers are like what's going on So in Python and probably most programming languages actually There's this concept of global and local so in this case This x is different to this x Okay, just like outside the temperature could be 40 degrees inside the temperatures 30 degrees They're both temperature, but they're different one's global one's local. I know it's a great analogy, but it kind of works now in Some cases what you want to happen is that you want this x and this x to be the same Now this is not good programming practice But people do it and it has its uses. So if I want this x to be the same as this x I need to use what's called the global keyword And I'm telling the computer that when you look for x when I do something with x I'm talking about the global x. We go ahead and run that You'll see now we have seven and seven because this x is now the same as that x So this is how global and local works outside of the function is the global space Inside this function is the local space local to that function So I had another function. It would have its own x unless I used global now. What's interesting if if you notice this is That Here I didn't have to use global because I'm just reading the value how it works is I get to print value It says print x and what does it looks around this local area first Do I have an x looks around doesn't see an x it says, okay. Well, all right Is there an excellent in the global space? Oh, there's an x I'll just go ahead and use that one and that is what it does How if we want to change it now watch what happens here, I do x plus equals five If I could do normally, so I'm adding five to x. Let's see what happens You see where we get this error. It says unbound local error, which isn't very helpful But local variable x referenced before assignment. So in this case, this is the equivalent of x equals x plus five Okay, and we can't do that. Okay, we can't add five to something that doesn't exist So let's put that back to that. So if I wanted to do this, I'd have to do global x and then it's gonna work just fine But then again, we've changed this x which has also changed this x because they are both the global x And that's the gist of global versus local. There's a few other rules You know, if you're using objects, it works a little bit differently If you're passing values, it kind of works a little bit differently as well But this is this is kind of the basic principle of it. You've got the global space Out here and you've got a local space as well. The thing the thing is that you have to remember to the computer This x and this x are Different So we can say that these two are different unless we explicitly tell the computer that we want to make this the same Now I said earlier, this was not good programming practice And what you would do if you if you wanted to do this the right way If you wanted to actually change x to b7 is you would probably do something like return x and then what you would do is You would say print value x you say x equals print value x And what that does is that returns this new x To the old global x the local x is return to the global x and then it's printed out This would be a better programming structure. It's just kind of the way you want to do things You really want to avoid global variables if you can You know for beginner programmers It's not such a big deal if you just want to get something working a little game or something But if you start making larger Programs you really really really want to avoid that situation because it causes a lot of bugs And it's hard to debug if you do have an issue. Hey, that's global versus local. I hope it helps Keep on coding. Scribe for updates. Ciao