 Welcome back everyone, my name is Brian. We're going to continue our journey into Python 3 with the global keyword. This is extremely cool, it's extremely easy, and it solves a complex problem. For example, we've seen this code before, x is 1, we have a function x is 6. If we run this, what's going to happen? Let's find out. So we have 6 and 1, basically x is two different variables and they're being treated different because it's two different scopes. Remember whenever you're defining a function, you're actually defining a scope. So what the global keyword allows us to do is actually modify variables at, well, you guessed it, the global scope. Let's dive in and take a look. Okay, in case you skipped that video on scope, what we're really diving into here is, well, blocks of code. We're defining a function which has its own scope. The problem is, Python is lexically or statically scoped, which means if we run this, we have two different variables. However, this is frustrating. If you comment this out, it's going to access the global scope. So what we want to do is to be able to access and modify, and that's what the global keyword does. So first things first, now that we understand that basic premise, let's go ahead and test this code. Sure enough, one one, we can access at a higher scope. Let's figure out how to modify. Let's go ahead and make a global variable. Ta-da, that's it. It's really that easy. Just because we put it on this very first line, there's no padding. This is just right at the edge of the file here. That is now considered in the global scope. Let's take a look at the scope issues in depth here. So let's say scope issues, and let's make a function called count, which is going to have some type of maximum number that we're going to count to. Now without the global keyword, Python is going to get very, very confused, very, very fast. Let's go ahead and demonstrate that. So I'm going to say counter equals, actually, let's do plus equals, we're just going to increment this right off the bat here. And sometimes Python will let you do it. Sometimes it's not. Other times it's going to just completely freak out and not have a clue what you're doing. But before we even run this, you can see right now it's saying, undefined variable counter. And this is perplexing because it's literally right here. So remember our conversation, we're accessing it. But now it's suddenly saying, whoa, it's not defined. Oh, that is frustrating. Let's go ahead and try and run that. So let's say count, and let's just try to count the one just to see what happens here. Oh, unbound local variable, local error, sorry, local variable counter reference before assignment, referenced before assignment really is our clue as to what's going on here. What it's saying is it is now making a new variable. And then trying to increment it before we've actually assigned it. So remember, Python types are a little bit, well, a mystery. We don't know if that's a string, a bull, actually as soon as it's created, it's an undefined. So if you take undefined, and you try to add one to it, what is the expected behavior? Remember, undefined is not zero. It's just simply there's nothing, literally nothing, it has no idea what type it is. So Python is going to get super confused, super, super fast. And let's just continue on with typing away here just to see how bad this can get. We're going to say counter is greater than or equal to the max. Then let's just go ahead and return false. Otherwise, we're just going to return true. See, same problem. So right here, it thinks it's just fine. It actually knows it's an integer. But right here, it's saying it's undefined. This is what I mean by Python gets very confused very, very quickly. Oh, that is frustrating. So we're going to fix this problem by saying global counter. And notice it's got the exact same name. So really what we're doing is we're saying use the global variable called counter. Give it the same name here. Save that. And as soon as you save it, you notice IntelliSense is smart enough to know that, hey, this is actually defined now and we can start working with it. And it knows it's an integer. Let's go ahead and just see the global keyword in action here. I'm going to make a variable called limit. It's going to be five. And I want to increment our counter using that. So I'm going to say 4x in range. I'm going to use our limit. Go ahead and get a variable from that function using that limit. Let's go ahead and print it out. I'm going to print out our counter. And just for giggles, let's go ahead and print out just done. So we can see it actually worked. Go ahead and run. Sure enough, boom, done. So it's working as expected now. And it's all because of this simple little keyword right here. Now, standard practice, I tend to avoid modifying global variables inside of a function for this very reason. For example, let's just say I were to grab this and make an entire new function out of it. And I go, huh, I don't need that global. I just want a variable called counter. Now I don't need that. Now suddenly, you guessed it, we are right back to the exact same issue. So now it gets very confusing about which counter we're talking about and in which function. You can very easily fix it by just doing this. But then, of course, now you're going back to modifying a global variable, which is not really a good programming practice. When in doubt, we're going to have a conversation about encapsulation later. But you want to encapsulate or use pretty much everything internal to your scope without going out modifying other scopes. I hope you enjoyed this video. You can find the source code out on github.com. If you need additional help, myself and thousands of other developers are hanging out in the Voidromes Facebook group. This is a large group with lots of developers and we talk about everything technology related, not just the technology that you just watched. And if you want official training, I do develop courses out on udemy.com. This is official classroom style training. If you go out there and the course you're looking for is just simply not there. Drop me a note. I'm either working on it or I will actually develop it. I will put a link down below for all three of those. And as always, help me help you smash that like and subscribe button. The more popular these videos become, the more I'll create and publish out on YouTube. Thank you for watching.