Skip to content

Python Setup ​

The SPIKE App has a built-in Python editor; the hub runs MicroPython (a slim Python). This page covers the environment, your first Python program, and how it relates to Word Blocks.

Why upgrade to Python ​

Word BlocksPython
Getting startedfastrequires syntax
Loop speedslowerfast (denser line-follow sampling = more stable)
FunctionsMy Blocks, no return valuesreal functions with returns
Mathpainful nested blocksone-line expressions
PID, data structuresbarelynatural
Version controlbinary project filesplain text, git-friendly

Suggested route: finish the Word Blocks track first (motors/sensors/control flow concepts) - everything transfers, only the notation changes.

New Python project ​

SPIKE App → New project → Python. Code editor on the left; the Console on the right shows print() output and error messages - a debugging tool Word Blocks never had.

This site uses the SPIKE App 3 API

LEGO rewrote the Python API in SPIKE App 3 (the import motor style). Many older tutorials online use the SPIKE 2 style (from spike import PrimeHub) - they are incompatible. On this site, the import motor / from hub import port style is the one to learn.

First program ​

python
import runloop
from hub import light_matrix, sound

async def main():
    await light_matrix.write("Hello")   # scrolls text; continues when done
    sound.beep(440, 300, 100)           # 440 Hz for 0.3 s
    print("done")                       # goes to the console

runloop.run(main())

Run it: the matrix scrolls "Hello", a beep plays, the console prints done.

The skeleton you must understand: async / await / runloop ​

SPIKE 3 Python is asynchronous. Any program that waits for an action to finish (which is nearly every robot program) uses this skeleton; purely synchronous scripts (just print, just lights) can skip it:

python
import runloop

async def main():
    ...   # your program goes here

runloop.run(main())

Three rules to memorize now (the advanced lesson explains the machinery):

  1. The program body lives in async def main():.
  2. Actions that "finish before continuing" (turn N degrees, finish scrolling text) get await in front.
  3. The last line runloop.run(main()) starts everything.

await motor.run_for_degrees(...) = the blocking "run for degrees" block; without await it's like the "start motor" block - the motor still starts, the program just doesn't wait. Note this applies to LEGO's native API only: your own async functions do nothing without await - details in the advanced lesson.

python
value = 42
print("reflection =", value)

Streaming sensor values to the console beats squinting at the 5x5 matrix:

python
import runloop, color_sensor
from hub import port

async def main():
    while True:
        print(color_sensor.reflection(port.C))
        await runloop.sleep_ms(100)

runloop.run(main())

Threshold calibration just got much more comfortable.

Exercises ​

  1. Run the first program.
  2. Deliberately misspell light_matrix, run, and read the console error top to bottom - reading errors is Python skill #1.
  3. Use the loop above to print your sensor's black/white values; compare with your Word Blocks-era calibration.

Next lesson: API Basics - crash-course syntax plus the SPIKE module map.

© 2026 FLL Knowledge Base · All rights reserved