Python-01

/

/

/

python/01-variables-and-basic-data-types

Variables and Basic Data Types in Python

Let's begin with absolute basics of python.

Python-01

/

/

/

python/01-variables-and-basic-data-types

Variables and Basic Data Types in Python

Let's begin with absolute basics of python.

Lesson Objectives

  • Python Rules

  • Basic Data Types

  • Variables (how to store data)

  • Simple Text Formatting

Are you Ready?

Summary

Hello pyRevit hacker’s World.
print("hello pyRevit Hacker's World!")
if ready_for_more:
    continue_watching()

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.

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)

'Text in python is called Strings'

"it can be written in a single or double quotes"

"""You can also use tripple quotes
to create multi-line
Text"""

'We can use "Double-Quotes" inside single quotes'

"And 'vice-versa' "

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')

We can not write text in python without quotes. It will break syntax rules
# Comments are ignored by python interpreters

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.

# We can not write text in python without quotes. It will break syntax rules
# Comments are ignored by python interpreters
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.

var = 'Text Data'
print(var)
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.

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.

my_name = 'Erik Frits'

print(my_name)
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.

#1️⃣ Variables Example

text_1 = 'Text in python is called Strings'
text_2 = "it can be written in a single or double quotes"
text_3 = """You can also use tripple quotes
to create multi-line
Text"""
text_4 = 'We can use "Double-Quotes" inside single quotes'
text_5 = "And 'vice-versa' "

# We can not write text in python without quotes. It will break syntax rules
# Comments are ignored by python interpreters
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.

#2️⃣Data Types in Python
string    = 'String means text'
num_int   = 10
num_float = 10.55
boolean   = True # True/False (similar to Yes/No in Revit)
empty     = None

print(string)
print(num_int)
print(num_float)
print(boolean)
print(empty)

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:

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.

print('Once upon a time, there was a Revit User named Erik')
print('Erik has spent months on soul-draining tasks in Revit')
print('Until one day Erik said: Enough!')
print('And by accident he discovered that he could automate his Revit work with python')
print('But Erik knew nothing about python.')
print('And so, Erik has begun his programming journey.')

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:

#4️⃣ How to reuse variables? And Why?
hacker = 'Josh'
app    = 'Blender'

print('Once upon a time, there was a {} User named {}'.format(app, hacker))
print('{} has spent months on a soul-draining tasks in {}'.format(hacker, app))
print('Until one day {} said: Enough!'.format(hacker))
print('And by accident he discovered that he could automate his {} work with python'.format(app))
print('But {} knew nothing about python.'.format(hacker))
print('And so, {} has began his programming journey.'.format(hacker))

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:

name = 'Erik'
age    = 29

print('My Name Is ' + name + ' and I am' + age + ' years old')

Or we could use format string.

name = 'Erik'
age    = 29

print('My Name Is {} and I am {} years old'.format(name, age))

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!

Questions:

Can I override variables?

Can I override variables?

What is a function?

What is a function?

What is a method?

What is a method?

Are there only 5 data types in python?

Are there only 5 data types in python?

Discuss the lesson :

P.S. Sometimes this chat might experience connection issues.

Use Discord App for best experience.

Discuss the lesson :

P.S. Sometimes this chat might experience connection issues.

Use Discord App for best experience.

Discuss the lesson :

P.S. Sometimes this chat might experience connection issues.

Use Discord App for best experience.

Unlock Community

The pyRevit Hackers Community is only available with pyRevit Hackers Bundle.
Upgrade Here to Get Access to the community and all pyRevit Courses.

Use coupon code "upgrade" to get 150EUR Discount as a member.

⌨️ Happy Coding!

Unlock Community

The pyRevit Hackers Community is only available with pyRevit Hackers Bundle.
Upgrade Here to Get Access to the community and all pyRevit Courses.

Use coupon code "upgrade" to get 150EUR Discount as a member.

⌨️ Happy Coding!

Unlock Community

The pyRevit Hackers Community is only available with pyRevit Hackers Bundle.
Upgrade Here to Get Access to the community and all pyRevit Courses.

Use coupon code "upgrade" to get 150EUR Discount as a member.

⌨️ Happy Coding!

© 2023-2024 EF Learn Revit API

© 2023-2024 EF Learn Revit API

© 2023-2024 EF Learn Revit API