Skip to main content

Lesson 8 · Hello, Python

Module 1 · Learning to Drive

Lesson 8 · Hello, Python

50–60 minPhase C · Transition to PythonBlockly → text
👩‍🏫

Teacher mode is on. Toggle it off (bottom-right) to preview the student view.

You've mastered the ideas — loops, functions, parameters — by dragging blocks. Now you'll write those same ideas as text, in Python, the language professional programmers and roboticists actually use. Nothing here is a new concept. It's the same thinking, typed instead of dragged.

Learning Objectives

By the end of this lesson you will be able to:

  • Explain why we move from Blockly to Python, and translate a block into the equivalent Python code
  • Follow Python's core syntax rules: indentation, colons, lowercase, parentheses, quotes
  • Use XRPLib to drive the robot from Python
  • Use print() to show messages and debug

The same program, two ways

Here's the Straight block you've used all module, and the Python that does the exact same thing:

straight blockthe block you know
from XRPLib.differential_drive import DifferentialDrive

drivetrain = DifferentialDrive.get_default_differential_drive()
drivetrain.straight(30)

Reading it line by line:

  • from XRPLib… import DifferentialDrive — bring in the robot-control library.
  • drivetrain = …get_default_differential_drive() — get the robot's wheels as an object you can command.
  • drivetrain.straight(30) — drive straight 30 cm. (Use a negative number to go backward, just like the block.)

The five syntax rules

Blockly never let you make a spelling mistake. Python will, so five rules matter:

  1. Indentation — spaces at the start of a line group code together. It's not decoration; it's how Python knows what's inside a loop.
  2. Colons — a : ends lines that start a block (for, if, def).
  3. Lowercaseprint is not Print. Names are case-sensitive.
  4. Parentheses — function calls need them: print(), even when empty.
  5. Quotes — text goes in quotes: print("hello").
Knowledge Check

Which line is written correctly?

Knowledge Check

What's wrong with this line? for i in range(4)

Activity · Write your first Python program

Blockly lived in the browser; Python lives in a code editor. We use VS Code with the MicroPython extension, which uploads your .py file to the robot and shows its console output. (Your teacher has it set up — see the XRP setup guide if you're on your own machine.)

Open VS Code, create a new file called lesson_08_first_drive.py, and type this in (typing, not pasting — it builds muscle memory). Then upload and run it:

from XRPLib.differential_drive import DifferentialDrive

drivetrain = DifferentialDrive.get_default_differential_drive()

print("Driving forward...")
drivetrain.straight(30)

print("Driving backward...")
drivetrain.straight(-30)

print("Done!")

Watch the console: your print() messages appear as the robot runs, so you can see exactly where it is in the program.

Knowledge Check

Why does drivetrain.straight(-30) drive the robot backward?

Activity · Read it, then fix it

Spotting mistakes is half of programming. What's wrong here?

drivetrain.straight(30)
Print("Backing up")
drivetrain.straight(-30)
Knowledge Check

Find the bug in the code above.

Try it: rewrite your forward/backward program to drive an L-shape — straight, turn(90), straight — with a print() before each step.

Real-world connections

Python is one of the most-used languages in the world, well beyond robots:

AI

Machine learning

Most AI research and tools — including large language models — are built in Python.

Science

Data & discovery

Scientists analyze experiments, genomes, and telescopes' data with Python every day.

Robotics

Real robots

From classroom bots to industrial arms, Python is a standard language for robot control.

Wrap-up

  • Why learn Python if Blockly works? (More powerful and flexible; it's what real programmers use.)
  • Name two of the five syntax rules. (Indentation, colons, lowercase, parentheses, quotes.)
  • What is print() good for? (Showing messages and debugging.)

Resources