Biton global keyword.
So you know that we are having multiple keywords that available in Python, but in this video we are
concentrating only on the global keyword in Python programs.
Global keyword allows us to modify the variable outside of the court in scope so that we can we can
change the variable value outside of the current scope.
And it is used to create a global variable and make changes to the variable in the local context also.
So how does the global keyword when we create a variable inside a function?
It's local is by default.
The scope is local by default.
When we define a variable outside of the function, it is global.
By default, you don't have to use the global keyword in that case.
So we use a global keyword to read and write a global variable inside a function.
Use of global keyword outside of the function has got no effect.
So if anybody were defining one variable outside of the function by default, it is having the global
scope.
Whenever we are defining one variable within the function, it is having the local scope.
But the point about this is that it is.
The third point is very interesting.
We use a global keyword to read and write.
A global variable inside are from the function itself.
So I think for the better idea, let us go for one practical demonstration to show you that how does
global keyword can be can be playing a magic in about scope definition of a variable.
In this demonstration we have defined one function that is a function, one where one variable has got
defined and initialize with a value 20.
Within this function, this variable X is having the effect of 20 here.
We're having one nested function within function one.
The nested function is having the name function to here.
We have defined one variable X and with the key word global.
So global X actually the global keyword will be used to modify a global variable here.
The X value has got updated with the value of twenty five here.
So this is a global one.
So here you have defined one variable which is of the scope globally.
So now before calling this function two, we are printing the value of X are calling the function to
now and we are called a function two and then after calling function two, the value of X is getting
printed.
So now let us see what is the outcome we are going to have.
So here you see here, this X is having the value twenty calling function to now after calling function
to the value of X has still with the value twenty because the changes were not going to get here.
We are expecting that the value might be changing, but no changes at all.
Is the X has got printed with the value twenty before and after calling the function to because this
particular X is having the global scope.
So if we have, if we want to print this particular X, then after calling this function we can directly
print this because this particular X is having the global scope that is not within this function to
our function.
One, it is outside of that.
So whenever we are trying to print the value of X from the from the outside of this function one, then
we are getting this value as twenty five.
I think you are getting this idea, this particular global X is actually we are defining one variable
X, which is having the global scope that we are going to define a global variable and that global variable
is getting updated or getting getting initialized with the value.
Twenty five here.
So this X and this X, they are actually corresponding to this X, so this X is still with the value
twenty.
So that's right.
We have got the output that's 20 and in the global X the value of X has become 25 here.
I think you have got the idea what is the purpose of this global keyword usage in this line to define
one global variable and to model with the help of the global keyword.
Thanks for watching this video.