Resources
Lesson Objectives
Python Rules
Basic Data Types
Variables (how to store data)
Simple Text Formatting
Are you Ready?
Summary
Hello pyRevit hacker’s World.
As you can see python code can be self explanatory and easy to read.
But you need to understand the basic rules of writing your code, which is called syntax.
Syntax is how you write instructions for the computer, like making sure to spell things right and put things in the right order with the correct spacing. It’s like grammar in English but for machine to read.
So let’s begin with the syntax basics starting with some basic data types
Data Types in Revit
In programming we can store our data using different datatypes. Think of Revit parameters for a second. When you create a new parameter, you need to select what kind of data it holds:
It can be Text, Integer, Length, Area, Yes/No and so on… That’s because each data types has its own rules and applications.
And so do programming languages have datatypes, and don’t worry there aren’t so many of them.
So let’s cover the basic python data types you need to know about, starting with Strings.
![](https://framerusercontent.com/images/LaWDJt73o3pI5fEvoEWYvbnyerA.png)
Setup pyCharm
Let's quickly create a pyCharm project together so we can write and test our code as we go. Firstly, make sure you have pyCharm downloaded and installed.
Then click on File-> New project…
Or if you've just freshly installed pyCharm, you will have a button to create a new project when you start it.
Next you need to select a location for your project (it can be anywhere) and choose interpreter type. Let's keep it simple and select project venv.
It means that it will create a venv folder inside of your python project. It's just a folder that will contain a copy of your python engine with the settings and packages that you might install later.
Don't overthink it, this is just general setup.
💡You can also use any python version. I use 3.10.6 because that's what I had on my PC installed by default. So feel free to download and install any version or use existing one on your machine.
Python Syntax Basics with Strings
Now, let's begin exploring python syntax rules with the basic data type called String. Strings in programming refer to text.
And to create such data-type you need to use 'single'
or "double quotes"
on each side of your message. That's the syntax rule for strings. (Similar to Revit formula syntax)
If you try to write your text without quotes, you are going to break a lot of python syntax rules. It will even highlight a few words with another color, because they have a special meaning ('not', 'in', 'break')
To avoid errors, you can place # hash-tag symbol in the beginning to make it a comment.
Comments in Python
While you write your code, everything has a meaning for computers to read.
Except for comments.
Comments allow you to leave messages for future self or other programmers to make your code more readable and easier to maintain. And as a beginner I recommend to leave as many comments as you feel necessary.
Variables
Alright, so we have some text data, but it's also doesn't make sense because it should be stored somewhere. That's where we need to use variables.
Variable is a container that holds your data.
Think of a box that has a label(var
) on it, and inside you can place something like numbers or text or a list of data ('Text Data'
)…
That box is a variable that can store some data. And to get what's inside this bos, you need to use box label name to refer to its data (var
)
Here is example of storing a simple text in a variable called var
. Later we can use its name to print the data it holds.
Variables in Revit
Let me demonstrate an example in Revit that will help you understand variables. Think of Global Parameters in Revit for a second. You can use them to create a global parameter that will hold some data, similar to a variable.
![](https://framerusercontent.com/images/OiE1sPrYNtJvBUstqokvWfqpH2c.png)
Then you can use this Global Parameter in other places to refer to the data it holds. And it can be used in multiple places.
Variables work the same in python.
You define it by writing a name without any quotes and then assign a data with equal sign. Then you can use this name to refer to the data it holds.
Think of variables as global parameters in Revit.
Variables in python
Now you understand what are variables, so let's go and assign all out text strings to different variables.
Variable can consist of alpha-numeric characters and underscores. Also variable can not start with a number, it always have to start with a letter.
Here is an example how we could assign all data to their own variables.
Basic Data types
Alright, so you know about variables and string data-type. What else is available.
Here are the 5 basic Data-Types you need to know about:
Strings (Text)
Integers (Whole numbers)
Floats (Numbers with floating point)
Booleans (True/False, similar to Yes/No in Revit)
NoneType (Empty Data)
Here is how to create variables that holds each of these data types.
There are more data types, but let's focus on these basic ones I've written above. In the next lesson you will learn more about other data types in python.
Here is a spoiler alert with python data types:
![](https://framerusercontent.com/images/fBuYgbedmwNyPpmHLWVWCgnrU.png)
Python Keywords
Remember how we wrote text without quotes and we had a few special words highlighted ? These were special keywords in python that have special meaning.
There are not so many of them, but it's important that you don't use the same names for your variables (Otherwise you might get some bugs in your code).
Here is a list of special keywords. No need to learn them by heart, you will naturally remember them as they are used in python for different concepts that I will teach you.
Also, on top of special keywords there are a few built-in functions in python. We will talk more about them in depth in another lesson, but think of print()
for example.
Functions in programming are used to trigger certain block of code to do something. Think of print as an example. It's used to display a message in the terminal where python is executed. You should also avoid using these kind of names.
💡But don't worry, modern code editors will highlight and remind you that you might be using a built-in function or a special keyword as a variable. And you will be able to just rename it or add underscore in the end to make it different.
How to Reuse Variables
Finally, let's have a look at why and how we could reuse our variables.
I will use a few print statements to create a small story.
You might notice that there is some repetition in this text like a person's Name
and the Software
. What if you decide to change it to something else? Or what if you want to have 2 similar stories but use different names?
Well, you could create variables to store these names and then reuse them in multiple places. Then, if you would want to make any changes, you would have to make change in one place.
Here is an example:
Now, we can just adjust hacker and app variable and it will make changes everywhere it's used. Again, think of Revit Global Parameters. If you change global parameter value, it will update this value everywhere this parameter is referenced.
And while Global Parameters in Revit are not so popular, we can't do much without variables in code.
String Formatting
Notice that I've used a special method called format
.
'…{}…'.format()
allows us to format the string by replacing placeholders ({}
) with another values provided in the parathesis.
You could join multiple strings (text) with a +
sign but that wouldn't be as readable and convenient as formatting a string. For example, we could write like this:
Or we could use format string.
It will have exactly the same results, but one is much better than the other. Isn't it?
Summary
So these are the basic syntax rules you need to understand with python.
You have data types like strings, integers, floats, booleans and NoneType for defining your data. And you need to store all of that in a variable, so you can reference this data across your code.
While in Revit you have a huge list of data types, it’s actually much shorter in python, but there are more types available. And we will look into Iterable data types in the next lesson. Think of them as collection data types to store a list of data.
But don't forget your homework before that.
HomeWork
Go over the code snippets shown in this lesson and try it out yourself.
Bonus point if you actually going to write it yourself instead of blindly copy-pasting. When you copy-paste you learn very little.
However, if you going to write it yourself it will help you understand these basics better. You might also encounter error messages if you make a mistake, and that's okay. Programming journey is impossible without seeing error messages, you just need to make sure you follow python syntax rules.
⌨️ Happy Coding!