1 00:00:00,420 --> 00:00:01,990 Biton global keyword. 2 00:00:02,460 --> 00:00:07,590 So you know that we are having multiple keywords that available in Python, but in this video we are 3 00:00:07,590 --> 00:00:12,270 concentrating only on the global keyword in Python programs. 4 00:00:12,510 --> 00:00:19,590 Global keyword allows us to modify the variable outside of the court in scope so that we can we can 5 00:00:19,590 --> 00:00:22,730 change the variable value outside of the current scope. 6 00:00:22,950 --> 00:00:30,130 And it is used to create a global variable and make changes to the variable in the local context also. 7 00:00:30,810 --> 00:00:35,940 So how does the global keyword when we create a variable inside a function? 8 00:00:35,980 --> 00:00:38,090 It's local is by default. 9 00:00:38,250 --> 00:00:40,040 The scope is local by default. 10 00:00:40,410 --> 00:00:44,220 When we define a variable outside of the function, it is global. 11 00:00:44,220 --> 00:00:49,500 By default, you don't have to use the global keyword in that case. 12 00:00:49,950 --> 00:00:56,280 So we use a global keyword to read and write a global variable inside a function. 13 00:00:57,150 --> 00:01:01,420 Use of global keyword outside of the function has got no effect. 14 00:01:01,740 --> 00:01:06,780 So if anybody were defining one variable outside of the function by default, it is having the global 15 00:01:06,780 --> 00:01:07,130 scope. 16 00:01:07,560 --> 00:01:11,660 Whenever we are defining one variable within the function, it is having the local scope. 17 00:01:12,210 --> 00:01:14,190 But the point about this is that it is. 18 00:01:14,190 --> 00:01:15,600 The third point is very interesting. 19 00:01:15,780 --> 00:01:18,390 We use a global keyword to read and write. 20 00:01:18,720 --> 00:01:22,960 A global variable inside are from the function itself. 21 00:01:23,220 --> 00:01:28,230 So I think for the better idea, let us go for one practical demonstration to show you that how does 22 00:01:28,230 --> 00:01:34,620 global keyword can be can be playing a magic in about scope definition of a variable. 23 00:01:35,400 --> 00:01:41,280 In this demonstration we have defined one function that is a function, one where one variable has got 24 00:01:41,280 --> 00:01:43,800 defined and initialize with a value 20. 25 00:01:44,340 --> 00:01:47,850 Within this function, this variable X is having the effect of 20 here. 26 00:01:48,360 --> 00:01:51,540 We're having one nested function within function one. 27 00:01:51,870 --> 00:01:54,950 The nested function is having the name function to here. 28 00:01:54,990 --> 00:01:58,620 We have defined one variable X and with the key word global. 29 00:01:58,620 --> 00:02:05,160 So global X actually the global keyword will be used to modify a global variable here. 30 00:02:05,160 --> 00:02:08,480 The X value has got updated with the value of twenty five here. 31 00:02:08,850 --> 00:02:10,740 So this is a global one. 32 00:02:10,750 --> 00:02:14,940 So here you have defined one variable which is of the scope globally. 33 00:02:15,180 --> 00:02:21,210 So now before calling this function two, we are printing the value of X are calling the function to 34 00:02:21,210 --> 00:02:27,420 now and we are called a function two and then after calling function two, the value of X is getting 35 00:02:27,420 --> 00:02:27,890 printed. 36 00:02:28,230 --> 00:02:31,140 So now let us see what is the outcome we are going to have. 37 00:02:31,710 --> 00:02:38,160 So here you see here, this X is having the value twenty calling function to now after calling function 38 00:02:38,160 --> 00:02:43,890 to the value of X has still with the value twenty because the changes were not going to get here. 39 00:02:44,310 --> 00:02:48,360 We are expecting that the value might be changing, but no changes at all. 40 00:02:48,360 --> 00:02:54,570 Is the X has got printed with the value twenty before and after calling the function to because this 41 00:02:54,570 --> 00:02:56,920 particular X is having the global scope. 42 00:02:57,210 --> 00:03:03,570 So if we have, if we want to print this particular X, then after calling this function we can directly 43 00:03:03,570 --> 00:03:09,420 print this because this particular X is having the global scope that is not within this function to 44 00:03:09,420 --> 00:03:09,990 our function. 45 00:03:09,990 --> 00:03:11,610 One, it is outside of that. 46 00:03:11,790 --> 00:03:17,970 So whenever we are trying to print the value of X from the from the outside of this function one, then 47 00:03:17,970 --> 00:03:20,250 we are getting this value as twenty five. 48 00:03:20,430 --> 00:03:25,710 I think you are getting this idea, this particular global X is actually we are defining one variable 49 00:03:25,710 --> 00:03:33,600 X, which is having the global scope that we are going to define a global variable and that global variable 50 00:03:33,600 --> 00:03:36,830 is getting updated or getting getting initialized with the value. 51 00:03:37,170 --> 00:03:37,930 Twenty five here. 52 00:03:38,280 --> 00:03:43,890 So this X and this X, they are actually corresponding to this X, so this X is still with the value 53 00:03:44,070 --> 00:03:44,630 twenty. 54 00:03:44,760 --> 00:03:45,440 So that's right. 55 00:03:45,450 --> 00:03:51,560 We have got the output that's 20 and in the global X the value of X has become 25 here. 56 00:03:51,840 --> 00:03:57,960 I think you have got the idea what is the purpose of this global keyword usage in this line to define 57 00:03:57,960 --> 00:04:02,260 one global variable and to model with the help of the global keyword. 58 00:04:02,790 --> 00:04:04,050 Thanks for watching this video.