1 00:00:00,009 --> 00:00:00,910 Welcome back. 2 00:00:00,920 --> 00:00:05,869 One last thing I want us to talk about before we round up the functions section 3 00:00:06,340 --> 00:00:10,340 will be the concept of variable scope. 4 00:00:11,149 --> 00:00:14,489 See in Python or in fact in any programming language, 5 00:00:14,500 --> 00:00:19,290 we have both local and global variables. 6 00:00:20,739 --> 00:00:25,899 Variables have the scope which will determine where they can be accessed. 7 00:00:26,389 --> 00:00:31,709 Now, variables that inside of a function will be local to that function 8 00:00:31,930 --> 00:00:37,700 and you cannot access them outside of that function. While variables 9 00:00:37,889 --> 00:00:41,000 that are defined outside of any function are global 10 00:00:41,340 --> 00:00:45,720 as an example. OK, X equals let's say 12, 11 00:00:46,330 --> 00:00:46,860 right? 12 00:00:47,220 --> 00:00:48,880 This right here 13 00:00:49,310 --> 00:00:52,439 is what we refer to as a global variable. 14 00:00:53,650 --> 00:00:55,220 Why? Because it's on its own, 15 00:00:55,919 --> 00:00:59,189 it's not inside any function or any loop or any el 16 00:01:00,279 --> 00:01:03,540 it's on it's own, it's just the X equals 12. 17 00:01:04,050 --> 00:01:04,980 However, 18 00:01:05,779 --> 00:01:12,839 if I created my own function and I said, define underscore modify a variable 19 00:01:13,550 --> 00:01:15,010 brackets, colon. 20 00:01:15,970 --> 00:01:19,550 And now I said X equals three, 21 00:01:22,089 --> 00:01:24,230 the X right here 22 00:01:24,519 --> 00:01:29,430 will be what we call a local variable because it is 23 00:01:29,440 --> 00:01:34,190 inside of the function which is modified on the score variable. 24 00:01:34,889 --> 00:01:39,910 So the global variable of X is 12, the local variable of X is three because again 25 00:01:40,029 --> 00:01:43,839 this three right here, it's inside all the modified VA function. 26 00:01:44,019 --> 00:01:44,510 So 27 00:01:44,720 --> 00:01:49,419 let's take a look at this, I'm going to tell my function to print out the value of X. 28 00:01:50,069 --> 00:01:50,910 And now 29 00:01:51,739 --> 00:01:53,669 I'm going to call the function, 30 00:01:54,220 --> 00:01:56,180 modify variable. 31 00:01:57,139 --> 00:01:59,669 What do you think the value of X here is going to be 32 00:02:00,569 --> 00:02:04,169 the value of X here is going to be three 33 00:02:04,300 --> 00:02:07,330 because we're simply calling the function but the I variable 34 00:02:07,440 --> 00:02:11,330 which already has the value of X to be equal to three. So if I run my function, 35 00:02:11,520 --> 00:02:13,729 you can see it's right there three. 36 00:02:14,309 --> 00:02:17,100 But what if I wanted to call 37 00:02:17,610 --> 00:02:21,309 the global variable X equals to I want to print out that value. 38 00:02:21,580 --> 00:02:25,050 All I need to do is just to call the default 39 00:02:25,399 --> 00:02:26,029 command 40 00:02:26,889 --> 00:02:28,470 a print function for 41 00:02:28,919 --> 00:02:29,869 Python. 42 00:02:30,020 --> 00:02:31,669 And then simply say print X. 43 00:02:32,570 --> 00:02:36,300 Now if I run the program again, you can see three and now 12 44 00:02:36,990 --> 00:02:41,089 again because this print right here, it's not the same print that's on the 45 00:02:41,380 --> 00:02:43,250 modify variable function. 46 00:02:43,259 --> 00:02:47,220 This print right here is meant to produce or print out the value 47 00:02:47,229 --> 00:02:51,550 of X which is the local value that's inside of the function, 48 00:02:51,559 --> 00:02:52,589 modify variable. 49 00:02:52,600 --> 00:02:53,539 However, 50 00:02:53,669 --> 00:02:57,169 this print X right here notice again the indentation, 51 00:02:57,490 --> 00:03:02,250 it is not inside of this function, it is outside. 52 00:03:02,539 --> 00:03:09,440 So the X here is going to be the one that belongs to the global variable, which is 12, 53 00:03:09,800 --> 00:03:15,199 that is the difference between global variables and local variables. 54 00:03:15,210 --> 00:03:16,119 If you wanted to 55 00:03:16,649 --> 00:03:19,289 get this particular value of X, which is three, 56 00:03:19,410 --> 00:03:22,080 you will need to always call the function 57 00:03:22,649 --> 00:03:25,889 that houses or that holds that particular value, 58 00:03:25,899 --> 00:03:27,350 which is of course modify variable. 59 00:03:27,800 --> 00:03:29,559 But the global variable, 60 00:03:29,750 --> 00:03:34,630 you can access it anywhere as long as it's not inside of the function. 61 00:03:34,839 --> 00:03:35,830 However, 62 00:03:36,649 --> 00:03:38,860 we can actually 63 00:03:39,059 --> 00:03:40,509 modify 64 00:03:40,720 --> 00:03:46,630 the value of our global variables inside of a function. 65 00:03:46,910 --> 00:03:49,300 Yes, we can do that. Check this out. 66 00:03:49,880 --> 00:03:51,669 I'm gonna go back to my function in here. 67 00:03:52,330 --> 00:03:52,949 All right. 68 00:03:53,320 --> 00:03:55,869 And just underneath, I'm gonna say global 69 00:03:56,660 --> 00:03:57,309 X 70 00:03:58,350 --> 00:04:01,369 just by using this statement called global, 71 00:04:01,529 --> 00:04:03,490 we can modify 72 00:04:03,820 --> 00:04:06,899 the global value of X. 73 00:04:07,110 --> 00:04:11,300 So now I'm saying global XX equals three. 74 00:04:11,570 --> 00:04:14,309 In this case, right now, we are simply 75 00:04:14,470 --> 00:04:15,960 modifying 76 00:04:16,260 --> 00:04:19,649 the global variable. That's what we're doing right now. 77 00:04:19,660 --> 00:04:20,769 We're simply going to modify 78 00:04:21,410 --> 00:04:24,489 the global variable of X. 79 00:04:25,040 --> 00:04:25,790 So 80 00:04:26,119 --> 00:04:27,010 now 81 00:04:27,559 --> 00:04:30,869 if I called, if I run my program, 82 00:04:31,109 --> 00:04:32,470 you can see right now 83 00:04:32,709 --> 00:04:35,589 that first of all the function modify variable 84 00:04:35,880 --> 00:04:37,630 X equals three will produce three. 85 00:04:38,079 --> 00:04:41,079 And because we have now modified the global variable 86 00:04:41,089 --> 00:04:44,609 of X to now be three instead of 12. 87 00:04:44,750 --> 00:04:48,459 Even when I call the print function that 88 00:04:48,470 --> 00:04:52,179 is outside of the modified variable function, 89 00:04:52,380 --> 00:04:54,480 it's still going to produce three. 90 00:04:54,489 --> 00:04:57,820 Because again, inside of this function, we modified 91 00:04:57,970 --> 00:05:00,140 the global variable X, 92 00:05:00,410 --> 00:05:06,399 we change the value from 12 to 3 by using the keyword global right 93 00:05:06,690 --> 00:05:07,380 here. 94 00:05:07,769 --> 00:05:10,630 So this is how you can also modify 95 00:05:11,160 --> 00:05:15,450 the global variables inside of a local function. 96 00:05:15,700 --> 00:05:18,700 Thank you so much for watching the video. I will see you in the next class.