- [Instructor] The title of this video is called variables and data types. I'm gonna start by showing you actually a much more complicated code than one that we would do in this course. It's a much, much longer code and it evolves things like classes, something we wouldn't get to until later course. The reason I want to just briefly look at this is to point out the fact that almost 50%, I would say 50%, of the code are variable names. And all these variable names are things that are just completely made up by the programmer. And I'm showing this because, for a number of reasons, one is that to say that how important variables are to the operation of code and that they're essential parts and ways that we use data around in the code. And so coming up with methods for naming things, with proper names, pneumonic names is important to add to the legibility of the code. The other reason I wanted to show it too is that you could look at this code and start to point out, because I'm using pneumonic naming devices, you could start to recognize things and, you know, sort of think about what they might be holding. So here I'm using something called create vector and I'm holding it in a variable called vec. This is a variable called newPt. It's probably holding a point. I have one called endPt and one called startPt. And because of two, the way that I write them, if you notice, I use this camel case with the capital P that that allows me to quickly see in the code that there is something that's probably a point being held. And I look to the beginning of that for a kind of prefix that tells me what sort of, what type of point that might be. And so using these devices throughout the code really allows you to sort of quickly see and identify what those variables might be holding. So these conventions that we use for naming, although they're not functional, remember there's nothing in how I write a variable beyond its repeatability that makes it functional. So this could be anything, but it's a good idea to use variable names that tell you a little bit of something about what they're holding, and that's why we stress using the conventions a lot. So let's just look at, start a code here and we'll look at well, how do we assign a variable? This is very simple. We just use the equal sign. So if I create a variable, let's say X, and I set that equal to 10. Now there's a little piece of the memory that has the label X on it. And I've stored within that little memory, if you remember the folder analogy, a folder is a little piece of memory. I put a label called X on it and put this value, this piece of data, into that folder called X. And then anywhere after that, within the code, if I call that back, let's say to print X, it's going to print out that value, whatever X is holding. After I've assigned that value, I could also say, you know, have X equal to, you know, a different number, 20. This is how we change, it's really the only way that we can change variables is to overwrite them. We either call it reassigning or overriding them. So I'm overriding whatever's held in X with this new value. And so now it's printing out 20. Something else that we'll see, probably it's gonna be more common when we get into things like iteration looping, where we'll have a variable that already exists, and we set that variable equal to itself, plus some other value. And that other value could be another variable, or it could be something that we hard code in. So let's say another 10. I run this, so that's giving me 30. So one question is how can we do this? How can we set a variable equal to itself? And the way we can do that is the way that Python understands this line, this expression, how it runs, is that we can think of that equal sign as a kind of firewall between these two sides of this expression. And what Python does when it sees that firewall, it's going to run what's on the right hand side first. So I can have an equation that involves that variable. So that's going to be the variable, it's gonna be 20, what it's currently holding. And it's going to use that, whatever the data that's holding within that equation, and then it's going to overwrite the variable X with whatever that equation is producing. So again, we'll see that quite a lot in our iteration when we start to cover iteration, because we're often looping through something and we're adding values to it over and over again. And so those values become compounded through a looping process. So we've been using the print function a lot to find out what's being held within variables. Now, something I need to point out about the print function and how we use it in this version of Python. This is actually a version of the version that we use in RhinoScript is a version called IronPython. And one of the differences between that, and let's say a regular version of Python 3, it's a minor difference, but it's an important one because I'll be using this print function quite a bit. Sometimes I can write print without using these parentheses. So I could just write print X and that's gonna work just fine. That won't always work in every version of Python. I think in Python 3, you have to put it within the parentheses. In this I can, as you can see, it's gonna work with either one. Again, another very important thing to reiterate about variables is that they have to be written exactly the same wherever I write them. So if I, instead write this X as a capitalized, capital X, and try to run that I'm gonna get an error. So this is a very, very common error that early programmers, this is one that you're going to to see a lot. You'll get this message down here that says, and this is where you're going to get your errors. And we'll get more into this and sort of reading what this, deciphering what this says in our lesson on debugging. But I'm sort of doing a debugging light here because this is a very common error. So this is saying that the name X is not defined. This is also my trace back here is giving me a little hint as to which line might be throwing that error in. So I looked to line five and this is where I put my capital X in. And so why it's saying it's not defined is, is because it doesn't understand what I mean by X here, because I haven't set it equal to anything. So the variable, the capital variable X has not been defined in the code. So Python doesn't know what I'm talking about when I write that. So it doesn't see this as the same as that. So these have to be written capitals, all those things matter, variables, the fact that I need to repeat it exactly the same as I've initially assigned it within the code is, is essential. So let's look at a couple of other ways that we might assign a variable. I've written out a couple of examples here. So we've already looked at a simple assignment. So where I just, we'll set a variable equal to something that I write. I essentially hard code into the code. It might be numbers. It could be strings. It could be any sort of data type that I said equal to a variable. The second one, which I'll call it input assignment, is where I'm using a function in RhinoScript. And this is one that we've seen before, GetObject, to input the ID of a curve. And I'm saving that ID in a variable called crvID. So this is properly pneumatically named, it gives me a sense of what it's holding. It's holding a curve, and what does it hold? It holds the ID or the GUID of that curve. And then a third type of assignment of a variable is that any time I call a function, it's going to return something and I can either choose to hold that returned value in a variable or not. So this is, and that's pretty much what this is doing. Although this is, I would be writing, I'm not inputting something from RhinoSpace. I'm just generating it purely within the code. So I produce AddLine. And if we look at, if we go and look at our help talk topics, and we can look up add line. Okay, and we can look exactly what it's looking for. So it's looking for a start and an end point, I think I showed this in a previous, in a previous lesson and it's going to return something. So it returns the ID of that newly created object. And so I'm saving that in this, in this variable called line. And so we can print these out. I can run this, it's gonna ask me to select a curve. Okay, and so I have X. I print it out here, equals 5, 12 and then I have my two IDs. So if now, if I choose to, the reason I'm saving these in a variable is that I might want to use them somewhere else within the code. So in this case, I'm just printing stuff out, but let's say, you know, I had this curve, this input curve, and I wanted to find the end point of that line. Well, I know there's a tool called CurveEndPoint and I can look down here and see what it's asking for. All it's asking for, well, the segment index is optional. I don't need to worry about that. So it's asking for the ID of a curve. I have that right here. So, or that's being held in that variable. And then I'm going to see what it's gonna return. So it's gonna return a 3D point of that curve if successful. Yeah, that's all it needs is that, so I can run this. Asking for the curve. I haven't, it's not broken, I'm not getting a sort of pink screen down here, works fine. But I haven't saved this with this returns in a new variable. If I wanted to do something with that, I would save it in, let's say, a variable I make up called end point. Print it out down here. I run it again. And now it's printing out the XYZ value, the coordinate values of that point, that it's found from that input curve. And it's done all of this using these variables that I've made up. So I'm gonna to end that here, I'm gonna talk just a little bit more about variables and data types in the next video.