Instructor: In order to finalize
our understanding of functions,
we have to talk about scope.
Another one of our key terms.
What is scope?
Let's have a look.
Scope is something present
in a lot of programming languages.
And scope simply means,
what variables do I have access to?
That's it.
So what do I mean by that?
Well, we've seen this before, right,
where I do print,
and then let's say name.
If I click run,
I'll get an error.
A NameError.
Name name is not defined.
Because, well, it doesn't exist, right?
And up to this point, we haven't really discussed this,
but this is because of scope.
What variables do I have access to?
The Python interpreter says,
hey, what do I have access to?
And when we use something that it doesn't understand
or it doesn't have access to,
it's going to throw a NameError,
or variable is not defined error.
And scope in Python has what we call functional scope
or function scope.
What does that mean?
Well, up until now,
when we create a variable like
total = 100,
this is part of what we call global scope.
That means anybody on this file
has access to this total variable.
I can use this inside of a conditional block.
I can use it inside of a function.
It has global scope.
But when I say Python has function scope,
what I mean is that when we create a function,
and let's say we create total as sum,
or let's say, some_func,
and we create total inside of the function.
If I print total here and I click run,
I get the same thing.
Name total is not defined.
This is because of python's function scope.
Anytime we create a variable,
if it's not inside of a function,
then it is part of the global scope.
We have access to it.
But you see over here we get that red underline,
undefined name total.
If let's say, for example, I had a condition,
if True,
then x = 10.
If I do this and I say x, and I click run,
I have 10.
Because, well, although I have indentation,
the only time I create a new scope,
you can think of this as a new universe
that we spawn.
Well, we can only do that
when we define a function like this.
So think of scope as a new world that we create.
In our case, when we create a function,
we create a new world
that anything that's indented inside of the function
is its own world
that we don't really have access to.
We can only use total
if we indent print
and it's part of this world.
That's what scope is.
Who has access to who.
So let's take a break here
and I wanna play a little game in the next video
where we try and figure out who has access to who.
I'll see in that one.
Bye-bye.