Skip to main content

Lesson 05 - What are Variables?

Variables in Python are just named pieces of information. Learn how to create and use them, building on concepts you already know from Revit, Excel, and Grasshopper.

Introduction

You've worked with parameters in Revit. You've used named cells in Excel. You've connected inputs in Grasshopper.

All of these are the same concept: giving a name to a piece of information so you can reference it later.

In Python, we call these variables.

This lesson shows you how to create and use variables in Python. By the end, you'll see that variables aren't some programming concept, they're just a way to name and store information, something you already do every day.


Lesson Overview

This section contains a general overview of topics you will learn in this lesson.

  • Understand what variables are and why they exist
  • Create variables using assignment
  • Learn Python's variable naming rules
  • Recognise why good naming matters
  • See how variables relate to BIM parameters and spreadsheet cells

What Is a Variable?

A variable is a named container for information.

Instead of writing 3.2 every time you need floor height, you write floor_height once and use that name everywhere.

floor_height = 3.2

This does two things:

  1. Creates a variable called floor_height
  2. Stores the value 3.2 inside it

Now, whenever you write floor_height in your code, Python knows you mean 3.2.


The Architectural Parallel

You already work with variables. You just call them different things.

In Revit:

  • Parameters store values (Room Number, Area, Level)
  • You name them once, reference them everywhere

In Excel:

  • Named cells or ranges store values
  • Formulas reference those names

In Grasshopper:

  • Number sliders have names
  • Components connect to those names, not the values directly

In Python:

  • Variables store values
  • Your code references those variable names

Same concept. Different tools.


Creating Variables (Assignment)

You create a variable using the equals sign =. This is called assignment.

project_name = "Community Center"
floor_count = 5
ceiling_height = 3.0
is_approved = True

The pattern is always:

variable_name = value

Left side: the name you're creating

Right side: the value you're storing


Why Variables Matter

Without Variables

print(12 * 3.2)
print(12 * 3.2 + 4.5)
print((12 * 3.2 + 4.5) / 12)

What happens when floor count changes from 12 to 15?

You have to find and change every 12 manually. And hope you didn't miss any.

With Variables

floor_count = 12
floor_height = 3.2
ground_floor_height = 4.5

total_height = floor_count * floor_height
building_height = total_height + ground_floor_height
average_height = building_height / floor_count

print(total_height)
print(building_height)
print(average_height)

What happens when floor count changes to 15?

You change one line: floor_count = 15

Everything else updates automatically.

This is the point of variables.


Variable Naming Rules

Python has strict rules about variable names.

Must Follow:

1. Start with a letter or underscore

floor_height = 3.2  # ✓ Valid
_temp = 5           # ✓ Valid
2nd_floor = 2       # ✗ Invalid (starts with number)

2. Only letters, numbers, and underscores

room_area = 42.5       # ✓ Valid
floor_2_area = 42.5    # ✓ Valid
room-area = 42.5       # ✗ Invalid (hyphen not allowed)
room.area = 42.5       # ✗ Invalid (dot not allowed)

3. Case sensitive

floor_height = 3.2
Floor_Height = 3.5
FLOOR_HEIGHT = 4.0

These are three different variables.

4. Cannot use reserved words

Python reserves certain words for its own use (guess what - we’ll be learning about these reserved words in the coming lessons :))

if = 5      # ✗ Invalid ('if' is reserved)
for = 10    # ✗ Invalid ('for' is reserved)
class = 3   # ✗ Invalid ('class' is reserved)

Variable Naming Conventions

Python doesn't enforce these, but the community follows them.

Use snake_case for variable names

Words separated by underscores, all lowercase.

# Good
floor_height = 3.2
room_name = "Conference Room"
is_fire_rated = True

# Bad (but technically valid)
FloorHeight = 3.2      # Looks like a class name
floorHeight = 3.2      # JavaScript style
FLOOR_HEIGHT = 3.2     # Looks like a constant

Use descriptive names

# Bad
h = 3.2
n = "Project"
x = True

# Good
floor_height = 3.2
project_name = "Project"
is_approved = True

You write code once. You read it hundreds of times.

Clear names save time.


Variables Are References, Not Boxes

This is important to understand.

When you create a variable, you're not creating a container. You're creating a label that points to a value.

floor_height = 3.2

Think of it like this:

  • The value 3.2 exists in memory
  • The name floor_height points to it

You can have multiple names pointing to the same value:

standard_height = 3.2
floor_height = standard_height

Now both standard_height and floor_height point to 3.2.

If you change one:

floor_height = 3.5

Only floor_height changes. standard_height still points to 3.2.


Updating Variables

You can change what a variable points to at any time.

floor_count = 5
print(floor_count)  # Output: 5

floor_count = 8
print(floor_count)  # Output: 8

The old value (5) is forgotten. The variable now points to 8.

You can even use the current value to calculate the new value:

floor_count = 5
floor_count = floor_count + 1
print(floor_count)  # Output: 6

This reads as:

  1. Get the current value of floor_count (5)
  2. Add 1 to it (6)
  3. Store the result back in floor_count

Variables in Action: A Real Example

Let's say you're calculating the total height of a building.

The manual way:

print("Ground floor: 4.5m")
print("Typical floors: " + str(8 * 3.2) + "m")
print("Total: " + str(4.5 + (8 * 3.2)) + "m")

Output:

Ground floor: 4.5m
Typical floors: 25.6m
Total: 30.1m

The variable way:

ground_floor_height = 4.5
typical_floor_height = 3.2
floor_count = 8

typical_floors_height = floor_count * typical_floor_height
total_height = ground_floor_height + typical_floors_height

print("Ground floor: " + str(ground_floor_height) + "m")
print("Typical floors: " + str(typical_floors_height) + "m")
print("Total: " + str(total_height) + "m")

Output:

Ground floor: 4.5m
Typical floors: 25.6m
Total: 30.1m

Same result. But now:

  • The logic is clear
  • The calculation is reusable
  • Changing floor count updates everything

Common Mistakes

Mistake 1: Using a variable before creating it

print(floor_height)
floor_height = 3.2

Error: NameError: name 'floor_height' is not defined

Fix: Define the variable first.

floor_height = 3.2
print(floor_height)

Mistake 2: Confusing assignment with comparison

floor_height = 3.2  # Assignment (creating/updating)
floor_height == 3.2  # Comparison (checking if equal)

The single = assigns a value.

The double == checks if two values are equal.

We'll cover == in a later lesson.


Mistake 3: Using spaces in variable names

floor height = 3.2  # ✗ Invalid

Error: SyntaxError: invalid syntax

Fix: Use underscores.

floor_height = 3.2  # ✓ Valid

Assignment

  1. Create a new file called project_variables.py
  2. Create variables for a building project:
    • Project name (string)
    • Client name (string)
    • Floor count (integer)
    • Typical floor height (float)
    • Ground floor height (float)
    • Project status (string like "In Progress" or "Complete")
  3. Use these variables to calculate:
    • Total building height
    • Average floor height
  4. Print the results using clear messages
  5. Change the floor count and run the script again. Verify the calculations update automatically.
  6. Experiment:
    • Try creating a variable with a space in the name. Read the error.
    • Try using a variable before defining it. Read the error.
    • Create two variables with similar names that differ only in capitalisation. Confirm they're different.

Knowledge Check

The following questions are an opportunity to reflect on key topics in this lesson.


Additional Resources

This section contains helpful links to related content. It isn't required, so consider it supplemental.

Updated on Mar 27, 2026