Before you can create objects, you need to store them. In this video, learn how to create and work with variables, including storing and handling Dynamo data to variables.
- [Instructor] To introduce you to Python, we're going to start with the basics. Gradually getting more complex as we work through the course. The first programming concept that we'll be learning about is variables. Variables are simply identifiers that point to a reserved space in memory. Which we can use to 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 them at any time. In programming variables act in much the same way.
So we can store data in memory, the storage unit. And later use variables, the address to access the data whenever it's needed. Let's switch back to Dynamo and learn how to use variables from within Python. As you can see I've created a new Dynamo file. Go ahead and place a new Python Script onto the workspace. And to start, let's simply input some data into Python and use a variable to store it. To do this let's search for a slider and place it onto our graph. And then we'll input this slider into input zero. And double click our Python Script to open it up.
To create a variable we simply need to name it. And then assign data to it using the equal symbol. So for example, let's create a variable named value overwriting data entry node. And we'll assign the incoming data import zero to it like so. This is known as assigning a value to a variable. The input data is now stored in the value variable. So let's output that variable and see what we get. Perfect. You can see we have output our variable which is storing the data that we're sending into the node.
We can also create data within Python by creating a variable and assigning a value to it. So for example we could create a variable named var1 which equals two. And we've created a number data type within Python. Or we could create a piece of geometry by assigning a point to var2. We now have point which is stored in memory which we can access using var2. Let's check both var1 and var2 by outputting the two together. Great. So we have the number two on our new point.
In Python we can reassign variables by simply using the same variable name. So for example, let's create var1 again and this time we'll assign it the number three. We can do this because Python is what's called a dynamically type language. Which means the variable and the data are not bound so the variable can store any data type. And as var1 equals three occurs after var1 equals two we'll get the value three if we output var1.
When creating variables in Python there are a 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, let's try creating the variable three var equals one. And then go ahead and accept. And you can see we have an error. Which is unexpected token var. This is because we have started the line with three. Which means Python doesn't recognize var as being a variable. We also can't use symbols in our variable names.
For example let's comment out our last error and we'll create the variable percent var. And go ahead and accept. And you can see we get the error unexpected token percent as Python does not expect the percent symbol to be at that position. Also be careful when using capitalization as Python is case sensitive. For example if we create two variables var1 and Var1 with capitalized V, equaling one and two respectively like so.
And then we'll output the two. You can see that we get two different numbers as we've created two different variables. This can often lead to unexpected errors. So it's something to keep in mind and an eye on when writing scripts. Finally in Python there are words known as keywords or reserved words that we simply can't use as Python has a use for them already. We know if a word is a keyword when it turns up red.
So already looking at our script we have two keywords at the top. These are import and from. If we try to use one as a variable, say from for example. And accept changes. You can see that we get another error. This being invalid syntax. These are some of the fundamentals that we need to know about variables. With this base understanding, let's start working with different data types in Python using variables to store them.
Note: This course uses Dynamo 1.3.1 and Revit 2018. For best results, it helps to have the same versions of the software.
- Importing the Dynamo Geometry library
- Defining variables
- Making decisions with conditionals
- Controlling flow with operators
- Defining functions
- Working with external libraries in Python
- Accessing the Revit API with Python