1 00:00:00,490 --> 00:00:04,540 Bite on global, local and non-local variables. 2 00:00:04,990 --> 00:00:07,810 So that means that variable depending upon the scope. 3 00:00:08,170 --> 00:00:13,090 We shall call it as a global variable, local variable and non-local variable. 4 00:00:14,170 --> 00:00:16,120 So let us discuss them one by one. 5 00:00:16,210 --> 00:00:19,570 So at first we are going for the global variables available. 6 00:00:19,660 --> 00:00:25,570 We just declared outside of the function or in global scope is known as the global variable. 7 00:00:26,350 --> 00:00:31,430 This means that the global biddable can be accessed inside or outside of the function. 8 00:00:31,480 --> 00:00:37,150 So that we debateable can be accessed within the function or from outside of the function because it 9 00:00:37,150 --> 00:00:44,770 is under the global scope, local variables, a variable which is defined or declared inside the functions 10 00:00:44,770 --> 00:00:47,860 body that is within the function body, well defined. 11 00:00:47,860 --> 00:00:52,750 One variable or in the local school is called the local variable. 12 00:00:52,990 --> 00:00:57,120 That is within the function body, though paintable has got defined or in the local scope. 13 00:00:57,310 --> 00:01:01,140 Then the variable will be known as the local variable syngas. 14 00:01:01,250 --> 00:01:02,080 A global writable. 15 00:01:02,140 --> 00:01:08,140 The variable will be will be defined or will be declared outside of the function in the global scope 16 00:01:08,380 --> 00:01:09,580 in case of local debatably. 17 00:01:09,600 --> 00:01:14,680 The variable will be defined or will be declared within the function or in the local school. 18 00:01:15,280 --> 00:01:21,520 What about, in that case, non-local variables, not local vegetables that are used in nested function 19 00:01:21,690 --> 00:01:24,640 whose local scope is not defined. 20 00:01:25,030 --> 00:01:31,390 So that is within the nested functions we are defining or we are using one variable which is not locally 21 00:01:31,390 --> 00:01:33,280 defined within the function itself. 22 00:01:33,670 --> 00:01:39,580 This means that the variable can be can be neither in the local nor in the global scope. 23 00:01:39,760 --> 00:01:43,210 So those variables will be known as non local variables. 24 00:01:43,480 --> 00:01:46,420 I think the conceptual conception is not getting clear to us. 25 00:01:46,660 --> 00:01:49,960 So let us go for one practical demonstration for debate and understanding. 26 00:01:50,740 --> 00:01:56,280 In this example, we have defined one X which has been defined outside of the function function one. 27 00:01:56,560 --> 00:02:01,180 So it is a global variable X and we just we just containing the string global here. 28 00:02:01,870 --> 00:02:07,300 So here within the function function one we have defined this global X step is referring to the same 29 00:02:07,360 --> 00:02:08,710 X variable here. 30 00:02:09,220 --> 00:02:11,220 Why is having the scope local. 31 00:02:11,380 --> 00:02:16,000 And it has got initialized with the local string X is goal to extend to. 32 00:02:16,090 --> 00:02:21,550 So X is having that string global start to means this globally will be repeated twice, that string 33 00:02:21,550 --> 00:02:24,880 will become global, global and that will be assigned to the X. 34 00:02:24,910 --> 00:02:25,510 Back again. 35 00:02:26,010 --> 00:02:28,720 We are going to print the value of X and Y respectively. 36 00:02:29,320 --> 00:02:31,180 So now we are calling this function one. 37 00:02:31,300 --> 00:02:36,520 But before calling this function one and after calling this function one, we have also printing the 38 00:02:36,520 --> 00:02:37,570 value of X here. 39 00:02:38,020 --> 00:02:39,100 So here is the outcome. 40 00:02:39,160 --> 00:02:45,370 You can easily find that initially that that is before calling this function one, the global X is having 41 00:02:45,410 --> 00:02:47,740 developed global within the function. 42 00:02:47,800 --> 00:02:50,320 It when it is printing X, it is printing global. 43 00:02:50,320 --> 00:02:50,770 Global. 44 00:02:51,220 --> 00:02:52,320 When it is printing Y. 45 00:02:52,480 --> 00:02:53,830 It is printing local. 46 00:02:54,220 --> 00:02:58,990 And nobody were printing this global ex back again after the execution of this function. 47 00:02:58,990 --> 00:02:59,260 One. 48 00:02:59,600 --> 00:03:05,570 We are getting this one as globally, globally, because this particular biddable ex is having the it 49 00:03:05,710 --> 00:03:07,420 has been defined with the global keyword. 50 00:03:07,780 --> 00:03:13,540 That means it is referring to the SIM X that is a global available defined outside of that function. 51 00:03:13,540 --> 00:03:14,180 Function one. 52 00:03:15,280 --> 00:03:16,900 So let us go for another example. 53 00:03:17,110 --> 00:03:19,990 That is a global and local biddable with the same name. 54 00:03:20,530 --> 00:03:25,810 Here you can find that it is going to fight is a global variable defined before the function function, 55 00:03:25,810 --> 00:03:26,160 too. 56 00:03:26,530 --> 00:03:29,980 And here you are defining another variable, A with the same name. 57 00:03:30,280 --> 00:03:37,030 But here it is having a local school so local available that access from the block where it is defined 58 00:03:37,090 --> 00:03:37,510 only. 59 00:03:37,840 --> 00:03:39,040 So here we are, Britain. 60 00:03:39,070 --> 00:03:40,440 This is the glue then. 61 00:03:40,690 --> 00:03:42,040 So print locally. 62 00:03:42,250 --> 00:03:45,160 If I print this value up a I shall be getting 10 here. 63 00:03:45,640 --> 00:03:49,530 So before calling this function to, I'm just printing the value of it. 64 00:03:49,600 --> 00:03:51,750 That is a globally I'm expecting here. 65 00:03:51,760 --> 00:03:53,800 The value of eight will get printed as five. 66 00:03:54,130 --> 00:03:59,290 And after this function two calling, if I print the value up A there is a globally I'm supposed to 67 00:03:59,290 --> 00:04:01,960 get the value five here back again. 68 00:04:02,260 --> 00:04:05,710 But within this particular function, to coin, I shall print this locally. 69 00:04:06,100 --> 00:04:08,270 I'm supposed to get the value at ten. 70 00:04:08,560 --> 00:04:12,400 So according to our expectation, you are getting the outcome as well. 71 00:04:13,150 --> 00:04:14,530 Let us go for another one. 72 00:04:14,770 --> 00:04:17,710 Well, it will be creating and using non-local variables. 73 00:04:18,250 --> 00:04:21,430 So we're here to a defined one function within the function. 74 00:04:21,430 --> 00:04:22,690 One variable is defined. 75 00:04:23,530 --> 00:04:26,320 This variable is local within the function itself. 76 00:04:27,190 --> 00:04:31,000 That means this variable is having the scope within the function itself. 77 00:04:31,480 --> 00:04:33,760 So now here we are having a nested function. 78 00:04:34,190 --> 00:04:37,270 So within the nested function, the name of the function is inner. 79 00:04:37,350 --> 00:04:40,000 We are supposed to define non-local variables. 80 00:04:40,360 --> 00:04:43,020 So here you can find that we have defined nonlocal X. 81 00:04:43,360 --> 00:04:48,070 That means we are defining the same X, which is defined here because it is nonlocal here. 82 00:04:48,430 --> 00:04:52,600 So X has got uprooted and from the inner from the inner method. 83 00:04:52,630 --> 00:04:54,290 We are printing the value of X. 84 00:04:54,520 --> 00:04:59,890 So you an expert in that X, will you get printed as non-local when executing this inanimate thought. 85 00:05:00,040 --> 00:05:01,660 So this code will get executed. 86 00:05:01,960 --> 00:05:03,910 X will get updated with the new string. 87 00:05:03,940 --> 00:05:04,960 That is a nonlocal. 88 00:05:05,230 --> 00:05:09,040 So after after executing this inner method, if I print this X. 89 00:05:09,370 --> 00:05:12,610 So you are expecting that we are supposed to get nonlocal here. 90 00:05:12,940 --> 00:05:18,940 And here you are executing the outer method after completion of the definition of the outer function 91 00:05:18,940 --> 00:05:19,150 here. 92 00:05:19,690 --> 00:05:21,240 So inner nonlocal. 93 00:05:21,280 --> 00:05:26,800 So from here, when we are printing the value of X, so X has got just initialized with the string nonlocal. 94 00:05:27,130 --> 00:05:29,110 So inner colon nonlocal is coming. 95 00:05:29,420 --> 00:05:31,780 But as X is a nonlocal available. 96 00:05:31,990 --> 00:05:37,290 So as it is set up that this X will also get updated with the string nonlocal. 97 00:05:37,750 --> 00:05:43,270 So as it is all up that this outer colon, outer colon X will also print nonlocal here. 98 00:05:43,840 --> 00:05:49,540 But in that same code, if I make this line, only if I make this one line under comment, then we are 99 00:05:50,110 --> 00:05:50,590 having it. 100 00:05:50,610 --> 00:05:52,650 This one has X is equal to non-local. 101 00:05:52,960 --> 00:05:59,040 So as this particular variable has gotten has got defined not without the nonlocal keyword. 102 00:05:59,290 --> 00:06:04,390 So as it is a of that, its scope will be local within the inert method only. 103 00:06:04,660 --> 00:06:08,190 So that's why he did not update this global X. 104 00:06:08,260 --> 00:06:11,980 It will just update that local X only within this inner method. 105 00:06:12,280 --> 00:06:13,950 So what will happen inertly? 106 00:06:14,020 --> 00:06:15,580 Print nonlocal from Dana. 107 00:06:15,880 --> 00:06:18,510 But it will be printing this outer colon x. 108 00:06:18,850 --> 00:06:20,780 It will print the string local. 109 00:06:20,830 --> 00:06:24,910 Back again, you can find that it is printing, nonlocal and local. 110 00:06:25,300 --> 00:06:29,130 So in this video we discuss the what is known locally available. 111 00:06:29,170 --> 00:06:30,330 What is global variable. 112 00:06:30,340 --> 00:06:31,930 How to define nonlocal available. 113 00:06:32,230 --> 00:06:36,240 How do different global variable into details and with demonstration. 114 00:06:36,910 --> 00:06:38,140 Thanks for watching this video.