Great.
You can now create basic Python programs using variables and functions.
But before we go any further, there is one thing you need to clearly understand and this is the scope
of a variable or also called the visibility of a variable.
So depending on where you create a variable, it may or may not be accessible from other parts of the
program.
And let's understand that with an example I'm just going to keep this file here.
And I'm going to add a few lines.
We're going to come back to this in a minute.
But for now let's write some new code here.
So I will define a new variable I will call it e for example is equal to three.
And I will define a function here.
Let's say print e okay.
Very dumb function.
Kind of useless but just for the purpose of this video and print e for example I use.
So basically I use the variable inside a function okay.
And let's call that here um print e okay.
Let's run that.
You can see we have three.
So we have the rest of the of the file here that's Also being executed.
But we have three here.
Print e.
Okay.
So we create a variable here.
We use it in the function.
Now I'm going to create another function.
Or actually I can just use this function.
And let's say I'm creating a new variable named test okay.
That value is ten.
And now let's say I want to print test okay.
So I create this variable inside this function.
And after that I try to print this variable.
So let's see what you can guess.
What would be the result of that.
Will we see ten.
And as you can see we actually have an error okay.
So the first three is printed because we could print the value.
But then when we try to print test you see test is not defined.
Why is that.
We just created test here and we can't use it here.
Well that is because of the scope.
So when you create a variable at the top of your program here or anywhere without any indentation,
you can use it everywhere.
After that, it is what we call a global variable.
But when you create a variable inside a block of code with an indentation.
So for example here inside a function.
So when you create a variable inside a block of code with indentation, this variable is a local variable
for this indentation.
Okay.
So we call this a local variable.
And so this test variable is created inside this function.
And we cannot use it outside of this indented block of code.
And after the block of code is executed here the variable is basically gone.
The only way to get access to that variable is between its creation here and the end of the block of
code, and the rule with scope is quite simple.
A variable will be visible in the scope it's created in, and in every nested scope inside this initial
scope.
For example, this variable we created here will be available well everywhere here and inside this function
because this is a nested scope okay.
Which means that we have an indentation.
It's a nested scope.
But the variable we create here is not available here simply because while this one is a more global
scope than this one.
So to go from here to there, we basically have to go back one indentation.
Okay.
If you go back one indentation, basically you lose the visibility of that variable.
And for now we only have one level of indentation.
But then as we progress you will see that we can have many more levels.
Okay.
Which means that we have more nested scopes.
And I want to show you something else that's linked to the scope.
And that's actually quite practical here for us actually.
So you can see we have those two, those two functions here triple number and print triple number.
And both of them have a number input parameter.
So what happens actually when you have an input parameter.
Well this is going to create a local variable inside the function.
And you can see actually when I click here this and that one is the same when I click here this one
and that one are the same variable.
So we create a local variable inside the function.
And as you know with the scope this here number variable is only going to be available in that function.
And this one is only going to be available in that function here.
So that's why we can have several functions with several input parameters.
And those parameters can actually have the same name, but they actually represent completely different
variables okay.
Because to go from this function to that function it's simple.
You are here.
So you need to go back one indentation.
Then you lose the well.
You lose the visibility because you need to go back one indentation and then go to that one and the
same on the opposite way.
To go from this function to that function, you first need to go out of the scope and then back in another
scope.
So then we lose the visibility.
So once again any parameter that you create in a function is basically going to create a local variable
inside that function that is only accessible in this function.
And also any nested scope that you create in that function.
So as a conclusion to this lesson, when working with variables, always keep in mind the scope of the
variables.
Is this variable declared in a more general or a more local scope, and in the scope that I am currently
writing in, can I get access to a variable?
And when you get used to it, it's very simple to spot the scope of any variable.