Python Blue Logo

Kickstart Python for Revit API (1)

I always find charming the feeling when you start learning something new, and nothing makes sense at the beginning. However, after some effort and time spent working on it, all the pieces come together. How sweet the change of mindset from “This is too much” to “Sky is the limit”! This is what happened to me when I started learning Python for Revit API, and I will share some basic concepts to start:

Expressions, values, and operators:

An expression is the most basic programming instruction you can give to a computer. For example:

7 + 8
15

In this case, 7 and 8 are values and + is an operator. The process to get to the result of that expression is called evaluation, and yes, that expression evaluates to 15. Another example:

“Revit” == “ArchiCAD”
False

This expression will evaluate to False, because, obviously, those two strings are different.

Data types:

In Python, values are the most basic elements used to create a program and they are categorized under different data types. We can consider them as bricks, the elemental material to build a house.

Data type in Python Example Data type in Dynamo Example
Integer 3, 8, -98, -7 Integer, Integer Slider 7, 8, -98
Float number 7.8, -33 Number, Number Slider 3.7, -10
String “Hello”, ‘Like’, “10” String “Dynamo”, ‘Parameter’
Boolean True, False Boolean True
NoneType None null null

All the previous data types are present in Dynamo, but if there is no node for it, Code Block will do the job.

Integers are integers, numbers without decimal positions (a bit of math refresh).

Floating or float numbers are numbers that have decimals.

42 == 42.00
True

Despite the differentiation between integer and floats, the previous expression will evaluate to True. That split is because of memory allocation to store values, integers take less space.

Strings are characters, and they need to be inside single or double quote marks, not inside the String node in Dynamo. Take into consideration that:

25 is not equal than “25”, one is an integer and the second value is a string.

Booleans are binary values that can only be True or False.

NoneType is a special type that represents that no value is present. In Dynamo, null values can be problematic as any action (method or function) called over it usually ends in error.

Operators:

Operators allow programmers to perform actions on values. There are several different kinds:

  • Math operators

They allow performing mathematical operations on values. Two of them work with strings, + and *.

Operator in Python Name Operator in Dynamo Example
** Exponent Code Block 2 ** 4
% Modulus or remainder % 31 % 8
// Integer division Code Block 31 // 8
* Multiplication * 3 * 4
/ Division / 54 / 8
+ Addition + 4 + 7
Subtraction 7 – 9

Addition performed on strings is called string concatenation:

“Revit” + “plus” + “Dynamo” + “is” +”awesome”
“RevitplusDynamoisawesome”

Note that spaces must be explicitly specified.

  • Comparison operators

As their name states, they are useful to compare two values.

Operator in Python Name Operator in Dynamo Example
== Equal to ==  1 == “1”, False
!= Not equal to != “Type” != “Instance”, True
Less than 4 < 8
Greater than 4 > 8
<= Less than or equal to <= 7 <= 10
>= Greater than or equal to >= 7 >= 7
  • Boolean operators:        
Operator in Python Name Operator in Dynamo Example
and and && True and True
or or || False or True
not not Not Not False

Evaluation table for and operator:

Case Evaluates to
True and True True
True and False False
False and True False
False and False False

Evaluation table for or operator:

Case Evaluates to
True or True True
True or False True
False or True True
False or False False

Evaluation table for or operator:

Case Evaluates to
Not True False
Not False True

Variables:

Variables are storage for values. Suppose you have obtained a value after some hard work and you don´t want to lose it. The way to store it and reuse it is through variables. The convention in Python is that variables must be a single word that starts with a letter, cannot contain special characters excluded the underscore (_) and not be a reserved keyword like def, None, True, False, etc

The PEP8, the official style guide for Python states that variables should be named like this:

Variable_name

However, personally I prefer the camel case style used in JS, tipping underscores is not a pleasant thing and is more legible this way:

varibaleName

Here is an assignment of a value to a variable:

xVariable = 34

From now on, xVariable value is 34. If we want to use it in another expression:

xVariable * 2 
68

This concludes the first part of Python basics; in the next section, we will see how we can use these building blocks to create a simple program.


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *