From the course: Dynamo for Revit: Python Scripting

Defining variables

- [Narrator] To introduce you to Python we're going to start with the basics. Gradually getting more complex as work through the course. There first programming concept we'll be learning about is variables. Variables are simply identifies the point to a reserve space in memory in which we can store values or data. Every time we create a new variable we're essentially reserving a space in memory to store something. Think of a variable as an address to a storage unit. You can use the storage unit to store items and you can return later to access those items at anytime. In programming, variables act in much the same way. So, we can store data in memory, that is the storage unit, and later use our variables, the address, to access the data whenever it's needed. In a new dynamite file, lets have a look at creating our own variables in Python. Go ahead and place a new Python script mode. To start, lets simply input some data into Python and use a variable to store it. To do this, lets search for the slider node, by hitting the right mouse button and typing slider, then selecting integer slider, and then input this into input zero. Then open up Python. To create a variable, we simply need to name it and then assign data to it using the equals symbol. So for example lets remove the code on line seven and we'll create a new variable named value and assigned to this the input at port zero. This is known as assigning value to a variable. The input data is now stored under the value variable. Lets output that variable by assigning to out and see what we get. Go ahead and hit run and look at the Python preview. Perfect. So you can see we have output our variable, which is storing the data we are sending in. We can also create data within Python by creating a variable and assigning a value to it. So for example we can create a variable named var1 and then assign to this one. Which means created a number data type within Python. Or we can create a piece of geometry, by assigning it to var2 for example. A point is now stored in memory. Which we can access by using this var2 variable. Lets have a look by assigning var2 to out and hitting run. Perfect. So you can see we have new point, which is stored under var2. In Python we can re-assign variables by simply using the same variable name. For example, lets change the number stored in var1 to two, by running var1 equals two. We can do this, because Python is what's called a dynamically typed language. Which means the variable and the data are not bound. So, the variable can store any data type. And is var1 equals two, because after var1 equals one we should get the value two when we output it. So lets output var1. Great, so we have two. As the variable represents the value it stores, they can be used directly in operations. Such as addition. To demonstrate this, lets make another variable, say var3 and we'll assign to this var1 plus two. As var1 is storing 2, var3 should now be storing four. So lets output var3 and lets have a look. Perfect. So it is storing four. When creating variables in Python, there are few naming conventions to look out for. Variable names can contain letters, numbers, and underscores however we can't use numbers to start. For example lets trying creating a variable 3var equals one and then hit run. This produces an error, which is unexpected token var. This is because we have started the line with three. Python doesn't recognize var as then being a variable. We also can't use symbols in our variable names. For example lets try %var equals two then hit run. And if we comment out the previous error, run again, you can see we still get an error, which is unexpected token percent. This is because Python does not recognize the modular symbol at this position. When creating variables, also be careful when using capitalization too. As Python is case sensitive. For example if we create two variables var1 equals one and Var1 equals two differentiated by the capital v letters we should have two variables. First comment out %var and then lets output var1, then Var1 with a capital v and hit run. And you can see we get the two different numbers as we've created two different variables. This can often lead to unexpected errors. So it's something to keep an eye on when working with variables. Finally, in Python there are words known as keywords, or reserved words that we simply can't use. As Python has use for them already. We know if a word is a keyword when it turns up red. So looking at our script, already we can see that there are two different keywords import and from. If we try to use one of these as a variable, for example, from equals one and hit run. You can see that we get the error invalid syntax. This is because keywords can't be assigned to. These are some of the fundamentals that we need to know when working with variables. With this base understanding, lets start working with some of the different data types in Python. Using variables to store them as we go.

Contents