In this tutorial segment, Akansha Yadav explains the concept of variable scope in Python, distinguishing between local and global variables. She defines scope as the region of code where a variable can be accessed and shows that variables defined inside functions or loops have a local scope, meaning they are only visible within that block. She demonstrates this with examples where attempting to print a variable defined inside a function (e.g., a, b, addition) results in a NameError because the variable is not defined in the outer scope. She then introduces global variables, which are defined at the file level and can be accessed from any function or later in the script. Using a simple example with a variable named `names`, she shows that a globally defined variable can be read inside a function and later in the program without error. The segment emphasizes why understanding scope is essential for writing correct Python code.
Chapters
Ladies and gentlemen, now we will see what is the scope of variable. So whenever the functions come in the picture, there are the division of variables. There are two types of variable. One is the local variable, another one is the global variable. So the scope of the variable, basically why we are discussing it, it is the region of the code where the variable can be accessed. Scope means where can that variable be accessed. So variables have different scopes. It totally depends on how and where you are defining it. Till now we were just defining the variables like normally writing a variable and storing some values in it, right? So now I will tell you two types of variables, two scopes of variables. One is the local scope, another one is the global scope. The local scope is the one where the variable is limited to some things. So if I will say over here, a and b was the parameter, but that was a variable for the function. a and b was the variable for the function. add is also the variable for the function. Let's say I'm writing addition and I'm returning addition. So addition, a and b, are limited to the function. Now I am trying to print addition. Oh wait, I have written the wrong spelling. Yeah, so it says name addition is not defined, although it is defined within the function. Because it is in the function, that is why it's saying it is not defined, because the scope of that particular addition variable is the local scope. It means it is defined within the function, so it can be accessed within the function only. So that is what the local variable is. If I will print— I will try to print a, then also I'm going to get the same error because a is also not defined, because that is defined within the function, that is limited to that function only. So that is what the local scope is. So those variables which we have defined within the function, or variables which is which we have defined within the loops— while loop, for loop— those are limited to that thing only. Another type of variable is the global variable, which we can access wherever. Like, if I am adding it— if I'm writing, let's suppose, names is equal to Vi. Just a normal word I am trying to store in it, and I'm going to use it within the functions. So now I'm getting— I'm, I'm using the names variable within the function also, and I am able to access that. If I'm going to access it later on also, I will get the output because that is the global variable. I'm not defining it in particular area, I'm just defining it for in general in my entire file. So that is what is the global scope of a variable is.