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 Blocks | Python | |
|---|---|---|
| Getting started | fast | requires syntax |
| Loop speed | slower | fast (denser line-follow sampling = more stable) |
| Functions | My Blocks, no return values | real functions with returns |
| Math | painful nested blocks | one-line expressions |
| PID, data structures | barely | natural |
| Version control | binary project files | plain 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
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:
import runloop
async def main():
... # your program goes here
runloop.run(main())Three rules to memorize now (the advanced lesson explains the machinery):
- The program body lives in
async def main():. - Actions that "finish before continuing" (turn N degrees, finish scrolling text) get
awaitin front. - 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.
print debugging
value = 42
print("reflection =", value)Streaming sensor values to the console beats squinting at the 5x5 matrix:
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
- Run the first program.
- Deliberately misspell
light_matrix, run, and read the console error top to bottom - reading errors is Python skill #1. - 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.