Bite on global, local and non-local variables.
So that means that variable depending upon the scope.
We shall call it as a global variable, local variable and non-local 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 we debateable can be accessed within the function or from outside of the function because it
is under the global scope, local variables, a variable which is defined or declared inside the functions
body that is within the function body, well defined.
One variable or in the local school is called the local variable.
That is within the function body, though paintable has got defined or in the local scope.
Then the variable will be known as the local variable syngas.
A global writable.
The variable will be will be defined or will be declared outside of the function in the global scope
in case of local debatably.
The variable will be defined or will be declared within the function or in the local school.
What about, in that case, non-local variables, not local vegetables that are used in nested function
whose local scope is not defined.
So that is 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 debate and 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 step is referring to the same
X variable here.
Why is having the scope local.
And it has got initialized with the local string X is goal to extend to.
So X is having that string global start to means this globally will be repeated twice, that 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 is before calling this function one, the global X is having
developed global within the function.
It when it is printing X, it is printing global.
Global.
When it is printing Y.
It is printing local.
And nobody were printing this global ex back again after the execution of this function.
One.
We are getting this one as globally, globally, because this particular biddable ex is having the it
has been defined with the global keyword.
That means it is referring to the SIM X that is a global available defined outside of that function.
Function one.
So let us go for another example.
That 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 you are defining another variable, A with the same name.
But here it is having a local school so local available that access from the block where it is defined
only.
So here we are, Britain.
This is the glue then.
So print locally.
If I print this value up 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 eight will get printed as five.
And after this function two calling, if I print the value up A there is a globally I'm supposed to
get the value five here back again.
But within this particular function, to coin, I shall print this locally.
I'm supposed to get the value at ten.
So according to our expectation, you are getting the outcome as well.
Let us go for another one.
Well, it will be creating and using non-local variables.
So we're here to a defined one function within the function.
One variable is defined.
This variable is local 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 inner.
We are supposed to define non-local variables.
So here you can find that we have defined nonlocal X.
That means we are defining the same X, which is defined here because it is nonlocal here.
So X has got uprooted and from the inner from the inner method.
We are printing the value of X.
So you an expert in that X, will you get printed as non-local when executing this inanimate thought.
So this code will get executed.
X will get updated with the new string.
That is a nonlocal.
So after after executing this inner method, if I print this X.
So you are expecting that we are supposed to get nonlocal here.
And here you are executing the outer 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 it is set up that this X will also get updated with the string nonlocal.
So as it is all up 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 one line under comment, then we are
having it.
This one has X is equal to non-local.
So as this particular variable has gotten has got defined not without the nonlocal keyword.
So as it is a of that, its scope will be local within the inert method only.
So that's why he did 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 Dana.
But it will be printing this outer colon x.
It will print the string local.
Back again, you can find that it is printing, nonlocal and local.
So in this video we discuss the what is known locally available.
What is global variable.
How to define nonlocal available.
How do different global variable into details and with demonstration.
Thanks for watching this video.