- [Instructor] Now that we've seen how to create
and refer to variables in Python, there's an important rule
that you need to understand regarding
how Excel makes those variables accessible.
So let's consider a worksheet with a Python cell in it
that declares a variable named s that contains a string.
So here, I've got my RowOrder_start example file open,
and in C5, I have a Python cell with a variable named s
that refers to this string.
Once I've defined this variable,
I can refer to that variable in all of the cells
that follow the cell where it's defined,
all of these cells going forward,
as well as all of the cells that come in the row
after that row.
And that's because Excel uses
what is called row order execution from left to right,
unless you're using a right to left language,
in which case it would be the other way around.
So I can refer to the s variable defined in C5 later on
in the sheet.
So for example, in G6, I can do something like this,
I can write =py, and then I can call s.upper.
And when I commit that,
sure enough, you can see that the string is there
in the cell.
But if I try to use the s variable prior to C5,
I'll get an error.
So for example, let's click in C4,
and let's make a python cell,
and I'll just refer to the s variable.
And you can see that when I do that,
I get an error, because it says name s is not defined.
The other thing you have to remember
is that Python also lets you re-declare variables.
This is a feature of Python,
it has nothing to do with Excel.
So you need to be careful
about using the same variable name later on
in the worksheet or the entire workbook.
So for example, here in C8, I've defined s,
or rather, I've redefined s to contain a different value,
in this case, the integer number 200.
So if I use s again in G8 for example,
let's make a Python cell here, refer to s.
And now, s is 200, because I've redefined it,
you can see I'm getting the new value.
So there's a couple of ways
that you can help prevent errors from happening.
One way is justifying all your variables
and import your modules as early as possible,
such as for example, in cell A1.
Now, of course, there might be reasons
you might not want to do this,
so another approach might be to create
a separate worksheet for your variable definitions
and any other Python modules you decide to import.
So for example, I can make a new worksheet,
and I'll call this Init.
And I'll move Init to be before Sheet1,
and I'll put that at the very beginning of the workbook.
And then, right here in cell A1,
I can just simply define my Python variable.
So in this case, I'll write =py,
and I'll just redefine s to be Hello World.
And the other thing that you might want to do,
this is a little bit of a trick that I do sometimes,
you might want to put a literal string at the end of the cell
to serve as a kind of programming comment,
so you know what the cell is doing.
So what I'm going to do
is I'm going to write in a string, "Initialize s."
And when I commit that,
you can see that what appears in the cell
is the last string that I put into the Python code,
because that's the last expression that gets evaluated.
s is still defined, you don't have to worry about that,
but visually, what happens is my little supposed,
you know, comment that I put in my cell.
All right, so let's commit that,
and let's go back to my worksheet.
And when I go back to my worksheet,
you can see that I can now just refer to the s variable,
because I've defined it in a previous worksheet.
You can see that I'm no longer getting an error in C4,
because I've defined the string in the previous worksheet.
So now, all of the following worksheets have access to it,
and so, that error is now gone.
So the easiest way to remember this rule
is that variables can be used
from the point where they are defined going forward.