WEBVTT

00:00.080 --> 00:00.440
Great.

00:00.440 --> 00:05.090
You can now create basic Python programs using variables and functions.

00:05.090 --> 00:12.410
But before we go any further, there is one thing you need to clearly understand and this is the scope

00:12.410 --> 00:16.070
of a variable or also called the visibility of a variable.

00:16.070 --> 00:23.390
So depending on where you create a variable, it may or may not be accessible from other parts of the

00:23.390 --> 00:24.140
program.

00:24.140 --> 00:28.250
And let's understand that with an example I'm just going to keep this file here.

00:28.250 --> 00:29.810
And I'm going to add a few lines.

00:29.810 --> 00:31.700
We're going to come back to this in a minute.

00:31.760 --> 00:33.680
But for now let's write some new code here.

00:33.680 --> 00:39.200
So I will define a new variable I will call it e for example is equal to three.

00:39.200 --> 00:41.900
And I will define a function here.

00:41.900 --> 00:44.360
Let's say print e okay.

00:44.390 --> 00:45.740
Very dumb function.

00:45.770 --> 00:52.640
Kind of useless but just for the purpose of this video and print e for example I use.

00:52.640 --> 00:56.810
So basically I use the variable inside a function okay.

00:56.870 --> 01:02.510
And let's call that here um print e okay.

01:02.510 --> 01:04.670
Let's run that.

01:04.940 --> 01:06.350
You can see we have three.

01:06.350 --> 01:11.640
So we have the rest of the of the file here that's Also being executed.

01:11.640 --> 01:12.960
But we have three here.

01:12.960 --> 01:13.710
Print e.

01:13.980 --> 01:14.250
Okay.

01:14.280 --> 01:15.540
So we create a variable here.

01:15.540 --> 01:16.920
We use it in the function.

01:16.950 --> 01:19.650
Now I'm going to create another function.

01:19.680 --> 01:22.470
Or actually I can just use this function.

01:22.470 --> 01:26.220
And let's say I'm creating a new variable named test okay.

01:26.250 --> 01:28.200
That value is ten.

01:28.200 --> 01:32.520
And now let's say I want to print test okay.

01:32.550 --> 01:36.570
So I create this variable inside this function.

01:36.570 --> 01:39.930
And after that I try to print this variable.

01:39.930 --> 01:41.670
So let's see what you can guess.

01:41.670 --> 01:43.170
What would be the result of that.

01:43.170 --> 01:45.180
Will we see ten.

01:46.620 --> 01:49.740
And as you can see we actually have an error okay.

01:49.770 --> 01:54.300
So the first three is printed because we could print the value.

01:54.300 --> 01:59.580
But then when we try to print test you see test is not defined.

01:59.580 --> 02:00.390
Why is that.

02:00.390 --> 02:03.750
We just created test here and we can't use it here.

02:03.750 --> 02:06.720
Well that is because of the scope.

02:06.720 --> 02:13.020
So when you create a variable at the top of your program here or anywhere without any indentation,

02:13.020 --> 02:14.880
you can use it everywhere.

02:14.880 --> 02:18.720
After that, it is what we call a global variable.

02:18.750 --> 02:22.960
But when you create a variable inside a block of code with an indentation.

02:22.960 --> 02:25.090
So for example here inside a function.

02:25.090 --> 02:32.950
So when you create a variable inside a block of code with indentation, this variable is a local variable

02:32.950 --> 02:35.050
for this indentation.

02:35.080 --> 02:35.230
Okay.

02:35.260 --> 02:37.000
So we call this a local variable.

02:37.000 --> 02:42.220
And so this test variable is created inside this function.

02:42.220 --> 02:47.020
And we cannot use it outside of this indented block of code.

02:47.020 --> 02:51.370
And after the block of code is executed here the variable is basically gone.

02:51.400 --> 02:57.640
The only way to get access to that variable is between its creation here and the end of the block of

02:57.640 --> 03:01.390
code, and the rule with scope is quite simple.

03:01.420 --> 03:09.790
A variable will be visible in the scope it's created in, and in every nested scope inside this initial

03:09.790 --> 03:10.360
scope.

03:10.360 --> 03:16.030
For example, this variable we created here will be available well everywhere here and inside this function

03:16.030 --> 03:18.430
because this is a nested scope okay.

03:18.460 --> 03:20.110
Which means that we have an indentation.

03:20.110 --> 03:21.220
It's a nested scope.

03:21.220 --> 03:28.060
But the variable we create here is not available here simply because while this one is a more global

03:28.060 --> 03:29.580
scope than this one.

03:29.580 --> 03:34.260
So to go from here to there, we basically have to go back one indentation.

03:34.290 --> 03:34.440
Okay.

03:34.470 --> 03:39.180
If you go back one indentation, basically you lose the visibility of that variable.

03:39.180 --> 03:42.180
And for now we only have one level of indentation.

03:42.180 --> 03:45.870
But then as we progress you will see that we can have many more levels.

03:45.900 --> 03:46.080
Okay.

03:46.110 --> 03:48.720
Which means that we have more nested scopes.

03:48.720 --> 03:51.930
And I want to show you something else that's linked to the scope.

03:51.930 --> 03:55.470
And that's actually quite practical here for us actually.

03:55.470 --> 04:01.680
So you can see we have those two, those two functions here triple number and print triple number.

04:01.680 --> 04:06.240
And both of them have a number input parameter.

04:06.240 --> 04:09.360
So what happens actually when you have an input parameter.

04:09.360 --> 04:14.310
Well this is going to create a local variable inside the function.

04:14.340 --> 04:21.060
And you can see actually when I click here this and that one is the same when I click here this one

04:21.060 --> 04:23.040
and that one are the same variable.

04:23.040 --> 04:26.430
So we create a local variable inside the function.

04:26.430 --> 04:33.540
And as you know with the scope this here number variable is only going to be available in that function.

04:33.540 --> 04:37.320
And this one is only going to be available in that function here.

04:37.320 --> 04:42.010
So that's why we can have several functions with several input parameters.

04:42.010 --> 04:48.220
And those parameters can actually have the same name, but they actually represent completely different

04:48.220 --> 04:49.480
variables okay.

04:49.510 --> 04:52.870
Because to go from this function to that function it's simple.

04:52.870 --> 04:53.890
You are here.

04:53.890 --> 04:55.960
So you need to go back one indentation.

04:56.380 --> 04:57.370
Then you lose the well.

04:57.370 --> 05:02.140
You lose the visibility because you need to go back one indentation and then go to that one and the

05:02.140 --> 05:03.820
same on the opposite way.

05:03.820 --> 05:10.030
To go from this function to that function, you first need to go out of the scope and then back in another

05:10.030 --> 05:10.690
scope.

05:10.690 --> 05:13.360
So then we lose the visibility.

05:13.360 --> 05:19.420
So once again any parameter that you create in a function is basically going to create a local variable

05:19.420 --> 05:23.140
inside that function that is only accessible in this function.

05:23.140 --> 05:27.850
And also any nested scope that you create in that function.

05:27.850 --> 05:33.820
So as a conclusion to this lesson, when working with variables, always keep in mind the scope of the

05:33.820 --> 05:34.690
variables.

05:34.720 --> 05:41.290
Is this variable declared in a more general or a more local scope, and in the scope that I am currently

05:41.290 --> 05:44.110
writing in, can I get access to a variable?

05:44.110 --> 05:48.520
And when you get used to it, it's very simple to spot the scope of any variable.
