How to create a numeric variable in Python (SPIKE) Pro Preview

To create a variable, it needs to have a name and a value that it stores within itself. The name of the variable can be any word, but when a program has many variables, it gets difficult to keep track of which variable does what. This is why it's good practice in programming to name variables according to what they do.

To access the full video please subscribe to FLLCasts.com

Subscribe

  • #2592
  • 26 Feb 2026

How to write a variable name

The rule of naming variables is to have at least two words so that we know what is in the variable and why. The names of the variables are written with an underline separation to make it easier to read (the underline is similar to a space). 

This variable naming standard is known as snake_case, since the words flow like snake vertebrae.

Example:

Let's look at an example of creating three variables with the names "motor_speed" and "motor_time":

motor_speed = 1000
motor_time = 2000

Where variables are created

"Global" variables are created after importing the libraries but before defining the functions such as "main()". The only thing that is written before the variables is the entered libraries that you will use in your code.

How to use variables

When we assign a numeric value to a variable, we can use its name instead of a number.

Example:

Let's look at an example in which we create and use the variables "motor_speed" and "motor_time" to set the speed and time of the motors' movement:

from hub import port
import runloop
import motor_pair

motor_speed = 1000
motor_time = 2000

async def main():
    # Pair motors on port B and E
    motor_pair.pair(motor_pair.PAIR_1, port.B, port.E)

    # Move using the variables
    await motor_pair.move_for_time(motor_pair.PAIR_1, motor_time, 0, velocity=motor_speed)

runloop.run(main())