Scope of variable in python | IMPORTANT Python concept | Cool Tip😎

Akansha Yadav shows how Python variable scope works: global variables are accessible everywhere, but local variables only exist inside their function. Trying to print a local variable outside its function throws a NameError.
Chapters
In today's video, we will discuss about scope in Python. So there, scope is related to variables. So there are two types of variables in Python. One is local variable, another one is global variable. Local variable are those variable which is accessed within the particular region. Suppose I am declaring any variable in function or a loop, so that is called local variable. But if I am declaring it in outside of those things, then this is— that is called global variable. So let's see what happens if I am going to access both the variables. Here I am writing a program storing global variable and I'm storing— 'I am' global variable then creating a function local variable there—in local variable I'm showing 'I am' local variable now trying to access both variables inside the function-inside-the-function-I-am-accessing-global-variable-and-inside-the-function-I-maX-thing-local-variable then—I'am calling my function—and testing it-with-after-calling-my-function-'I am going-to-access'-the-global-variable-and-local-variable-now-trying-to-access-both-the-variables-or-I'm-printing-the-lines-again-outside-the-function-and-this-is-going-to-be-for-the-global-variable so that's what we can do here; you see! Here also same thing happening again like first accessing this value from outside Now of our parameterized functions which have been passed values into them through parameters I or arguments if am they're passing any argument by default at least trying one keyword will be provided for example var name="Akanksha Yadav" because some kind of data type to, must pass when using params method right? So similarly "var city='Kolkata'" giving something called object format over here OK?! you And finally know, printing out those two lines okay... Now try running your program once more time for the local variable also, but I am writing a comment to it. Attempt to access the local variable outside the function will result in an error, which is going to be true. So I will show you after uncommenting the line that I am going to write down. This is the line that I am going to comment later on. So here I am going to comment this line number 18 as of now. Then we'll show you what is the difference between both the things. So let's run the program. Uh, let's make it commented. So here we go with the hashtag in it, and then going to run the file. Once I will run the file, we should get 4 print statements— 3 print statements as of now. So here I am getting first inside the function, then again inside the function, and then outside the function, which is going— which is good for us, right? This is what we have written in our code. Now I am going to uncomment the line number 18 and test it whether it will give us the local variable or not. So let's run the file again. And here we got 3 print statement, and after that we are getting our traceback error, which is a name error that we are getting. And we are saying name local variable is not defined, although we have defined it, but that is a local variable with— because we have defined it inside the function.










