Biton, global, local and non-local variables, so that means the variable, depending upon the scope,
we shall call it as a global variable, local variable and nonlocal variable.
So let us discuss them one by one.
So at first we are going for the global variables available.
We just declared outside of the function or in global scope is known as the global variable.
This means that the global biddable can be accessed inside or outside of the function.
So that very variable can be accessed within the function of outside of the function because it is under
the global scope, local variables available which is defined or declared inside the functions body
that is within the function body where defined one variable or in the local scope is called the local
variable that is within the function body.
The optimal has got defined or in the local scope.
Then the variable will be known as the local variable.
So in case a global variable, the variable will be will be defined or will be declared outside of the
function in the global scope in case of local variables, the variable will be defined or will be declared
within the function or in the local scope.
What about, in that case, nonlocal variables, not local variables that are used in nested function
whose local scope is not defined?
So that means within the nested functions we are defining or we are using one variable which is not
locally defined within the function itself.
This means that the variable can be can be neither in the local nor in the global scope.
So those variables will be known as non-local variables.
I think the conceptual conception is not getting clear to us.
So let us go for one practical demonstration for the better understanding.
In this example, we have defined one X which has been defined outside of the function function one.
So it is a global variable X and we just we just containing the string global here.
So here within the function function one, we have defined this global X that is referring to the same
X variable here.
Y is having the scope local and it has got initialized with the local string, X is equal to two.
So X is having the string global two.
This global will be repeated twice, the string will become global, global and that will be assigned
to the X back again.
We are going to print the value of X and Y respectively.
So now we are calling this function one.
But before calling this function one and after calling this function one, we have also printing the
value of X here.
So here is the outcome.
You can easily find that initially that that means before calling this function one, the global X is
having the value globally within the function.
When it is printing X, it is printing global, global.
When it is printing, why it is printing local.
And whenever we are printing this global expec again after the execution of this function, one, we
are getting this one as global global because this particular biddable ex is having the it has been
defined with the global keyword.
That means it is referring to the same X that is a global variable defined outside of the function function
one.
So let us go for another example.
There is a global and local biddable with the same name here you can find that it is going to fight
is a global variable defined before the function function, too.
And here we are defining another variable with the same name.
But here it is having a local school.
So local variables that access from the block where it is defined only.
So here we have written this is the gluten.
So print locally.
If I print this value of a, I shall be getting 10 here.
So before calling this function to I'm just printing the value of it.
That is a globally I'm expecting here.
The value of it will get printed as five and after this function to call if I print the value of it,
there is a globally I'm supposed to get the value five here back again.
But within this particular function, two point, I shall print this locally.
I'm supposed to get the value estin.
So according to our expectation, you are getting the outcome as well.
Let us go for another one where it will be creating and using non-local variables.
So we're here to a different one function within the function.
One variable is defined.
This variable is locally within the function itself.
That means this variable is having the scope within the function itself.
So now here we are having a nested function.
So within the nested function, the name of the function is not supposed to define non-local variables.
So here you can find that we have defined nonlocal X.
That means where referring the same X, which is defined here because it is nonlocal here.
So X has got uprated and from the inner from the inner method printing the value of X.
So we are expecting that X will get printed as nonlocal when executing this in our method.
So this code will get executed, X will get updated with the new string that is the nonlocal.
So after after executing this inner method, if I print this X, so we are expecting that we are supposed
to get nonlocal here and here you are executing the order method after completion of the definition
of the outer function here.
So inner nonlocal.
So from here, when we are printing the value of X, so X has got just initialized with the string nonlocal.
So inner colon nonlocal is coming, but as X is a nonlocal available.
So as a result of that, this X will also get updated with the string nonlocal.
So as a result of that, this outer colon, outer colon X will also print nonlocal here.
But in that same code, if I make this line, only if I make this line under comment, then we are having
this one as X is equal to nonlocal.
So as this particular variable has gotten has got defined not without the nonlocal keyword.
So as a result of that, its scope will be local within the innot method only.
So that's why it do not update this global X, it will just update that local X only within this inner
method.
So what will happen inertly?
Print nonlocal from the inner button will be printing this out.
Colonics.
It will print the string back again.
You can find that it is printing, nonlocal and local.
So in this video we have discussed the what is nonlocal variable, what is global variable, how to
define nonlocal variable, how to define global variable into details and with demonstration.
Thanks for watching this video.