 Hey, what's going on guys root from no shell.com here today, and we're looking at even more Python today We're gonna be checking out Python's Function scope and this isn't like only exclusive to Python or anything This seems to be a common thing in like all programming languages and all computer languages like C++ Python Java PHP you name it if it's scope is something that I feel like it makes perfect sense But it might be a little hard to think of at that same time The the idea is when you define a function the things that you create inside that function are Only exclusive are only accessible and like usable and seen invisible whatever word you want to use They're only seen inside that function and any functions Below that and now you can think of below as sort of like layers like as if you had maybe an onion I don't know but just each each piece of it You have the global scope where like the main function runs and the variables that are created in there will be accessible from the Functions inside that the functions inside the global scope will not be able to have variables that are seen inside the global scope See when I explained it and I speak it I talk about it verbally it it might be a little hard to picture but if we open up idle and We create a new program a new window. I'm gonna Get my shebang started environment in Python I'm gonna save this as a file Python there we go overwrite that let's do Let's Let's run a function And we'll define get info We don't have any parameters. I'm gonna make a little bit of white space here And then we'll run get info So now if we do kind of what we did in the last video if we do user input equals raw input What should we say and then print Let's say tab and Then user input. We run this. What should we say? Oh, I should have made a new line there Urca burglar. Let's say Urca burglar Now when we go back to the program, I'm just gonna add the new line here. So it works. Okay, if We outside of the get info function tried to display The user input variable It's gonna like have a heart attack. Give it a go Let's run a function. Whoa. Whoa. Whoa. What's the matter here name error? The name user input is not defined in line 10. So we're on line 10 I can see that down at the bottom of vital and printing user input It's it's gonna give me an error because user input is not in this scope. It's only inside the get info function this one right here But that's interesting. What if we what if we had X? Let's say in the in the uppermost layer X equals 4 and then what if we printed X Inside the function. Well, that makes sense Let's comment out this line here in case we ever need it again. I really doubt it But let's hit a five get our program going. Let's run a function. What should we say her peder peder? Let's say her peder peder and then it displays for Huh, okay? So we have that sort of layered structure any any variables that are above These systems and these methods and these functions will be accessible in those methods and those functions and those things But any anything any variables below of another function will not be able to call those because they aren't created in that scope Huh, does that make sense? When you see it in action, it's it's a lot different from hearing it or maybe when you read it and you have a visual display or that sort of thing All those things do not work. It's strange. What if we did? What if we did X equals 4 and here? Actually X equals 5 Then we did X equals 4 actually X equals 3 over here Let's print X outside the function first time Let's print X inside the function once and then let's print it again after we've set it inside inside the function and Then let's print it outside the function once more and Then once we reset it. We will print it again So we're just printing X a whole bunch of times. So it looks a little dumb, but hey All right clean that up a little bit and yeah, that's that looks good to me User input might actually give us a problem. We should kill that f5. Okay. Let's run a function for okay, that's good. We print it again. Let's See what should we say? Wait a second. Oh, I see I see because we did we called get info after we reset things. Okay, let's try that again after Okay, so now if we do get info before if we can see up at the top here We're setting X to 4 at first and we print out X. So X is 4 that makes sense. So what should we say? Let's just say John Madden Football I'm just kidding Oh, oh, oh local variable X reference before assignment So let's print X What line is that 9-11? Are we on line 11? Yeah, we are interesting So now if we get rid of this though If we don't at least set it it should still be let's give it a go again. What should we say? Football We get four Okay, so it's letting us use that Above that it's letting us use the the X that's above the function But if we were to set X inside the function, it has a problem with that We can only display it after we set it if we create a new one Let's try it again Here comes another Chinese earthquake Let's say here comes another Chinese earthquake. We get five X equals five. We print X Okay, now we get that five and then we're printing X again because X is no longer five in the scope It's still four because we haven't modified it outside of the get info function And then we have X equals three at that last bit and it changes back to X equals three Huh Okay, that's curious. That's that's interesting the rules that you have to be able to Declare X inside the function without using the the one of the layer above that and if you're going to display it if you're Going to reset it and that sort of thing so that might take a little bit of time to be able to understand but I Feel like it makes sense after after you look at it in a grander sense and in a bigger picture I don't know. That's just that might just be me but The more you practice with it the more you play with that idea the more prosperous you will become and That's kind of the idea that I want you guys to have after watching this video today Thank you guys for watching. It'd be awesome if you could give me a like maybe leave a comment Maybe subscribe. I don't know that that's that's you're doing but I'll see you again in the next video Bye if I can get my goddamn terminal up the hell