Lesson 1-8 · Hello, Python
Lesson 1-8 · Hello, Python
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 the first three of Python's five syntax rules: lowercase, parentheses, quotes
- Use the three-step XRPLib pattern — import the class, make an instance, call its methods — to drive the robot from Python
- Use
print()to show messages and debug
Why switch to Python at all?
Blockly wasn't training wheels. Every idea you've used so far — loops, functions, parameters — is a real programming idea, and you learned it without spending a single minute on typos. That was the point: get the thinking first.
But Blockly can only offer you the blocks somebody built for it. Python is a general-purpose programming language, which means it isn't tied to robots, or to any one job. It's one of the most widely used languages in the world, and the same Python you're about to write also shows up in:
- Web development — the servers behind sites and apps
- Data science — analyzing and charting real data sets
- Machine learning and artificial intelligence — most of the field's everyday tools are written in or driven by Python
- Automation — the scripts that do a tedious job so a person doesn't have to
- and plenty more, from scientific research to finance to visual effects
The practical reason matters just as much. As programs get bigger, the thing that saves you isn't writing faster — it's not having to write it at all. Python gives you access to an enormous body of software libraries and frameworks: code other people have already written, tested, and handed over for anyone to use. Need to read a sensor, talk to a website, do statistics, or draw a graph? Somebody has almost certainly published good code for it, and using it is usually a single import line.
You're about to meet exactly that in the next section — XRPLib, the library that
makes your robot's motors and sensors a few words of Python instead of a semester
of electronics. It's the same deal the professionals get, just aimed at a robot
that fits on a desk.
What's the main advantage of a general-purpose language like Python over Blockly?
The same program, two ways
Here's the Straight block you've used all module, and the Python that does the exact same thing:
← the block you knowfrom XRPLib.differential_drive import DifferentialDrive
drivetrain = DifferentialDrive.get_default_differential_drive()
drivetrain.straight(30)
Reading it line by line:
from XRPLib.differential_drive import DifferentialDrive
Bring in the robot-control library.
drivetrain = DifferentialDrive.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.
What's a library?
XRPLib is a library — code somebody else already wrote, tested, and left on
the robot for you to use. Making a motor turn a wheel at a steady speed means
timing electrical pulses and counting encoder ticks; reading the reflectance
sensor means measuring how much light bounces back and turning that into a number.
That work is genuinely hard, it's already done, and it's sitting in the library.
Picture a shelf of books, one book per piece of hardware — one for the drivetrain, one for the reflectance sensor, one for the rangefinder, one for the servo. You don't read the whole shelf. You take down the one book you need, when you need it.
That's what import does. This line:
from XRPLib.differential_drive import DifferentialDrive
takes the DifferentialDrive book off the shelf and opens it on your desk.
What's written inside are the things a drivetrain can do: straight(), turn(),
set_effort(). Leave the book on the shelf and Python has no idea those words
mean anything.
You import each device as you need it, which is why the import lines at the top of a program are a decent table of contents for what the program touches.
Three steps — and they never change
Those three lines aren't a driving recipe. They're how you use any piece of hardware in XRPLib, so once you have the shape you can read code for a device you've never touched:
1 · Import the class.
from XRPLib.differential_drive import DifferentialDrive
This tells Python where the code for that piece of hardware lives. Nothing happens to the robot yet.
2 · Make an instance.
drivetrain = DifferentialDrive.get_default_differential_drive()
This hands you an object that stands for the real hardware bolted to your robot.
The name on the left is yours to pick — drivetrain is just a good one.
3 · Use it.
drivetrain.straight(30)
Instance name, a dot, then what you want it to do. That instance.method() shape
is most of the Python you'll write from here on.
Steps 1 and 2 happen once. Step 3 happens all day.
Numbering them 1, 2, 3 makes it look like three lines you write together. They aren't, and a real program makes the difference obvious:
from XRPLib.differential_drive import DifferentialDrive # 1 - once, at the top
from XRPLib.board import Board # 1 - once, at the top
drivetrain = DifferentialDrive.get_default_differential_drive() # 2 - once
board = Board.get_default_board() # 2 - once
board.wait_for_button() # 3 - use it
print("Driving forward...")
drivetrain.straight(30) # 3 - use it
drivetrain.turn(90) # 3 - use it
drivetrain.straight(30) # 3 - use it again, and again
print("Done!")
Steps 1 and 2 are setup. They belong at the top of the file, they run once, and you never repeat them — importing the same class twice or making a second drivetrain gains you nothing. Step 3 is the actual program. It might come twenty lines later, it might come inside a function you write in Lesson 1-11, and it will happen as many times as your robot has things to do.
So when you see the three steps stacked together below, read them as a summary of the shape rather than a block to copy. Every one of them says: set this up at the top, then use it wherever you need it.
The device changes; the three steps don't. Here is the reflectance sensor you
meet in the next module — read it against the drivetrain code above. The notes off
to the right are comments: a # tells Python to ignore the rest of that line,
so everything after it is there for the human reading the code, not for the robot.
from XRPLib.reflectance import Reflectance # 1 - import the class
reflectance = Reflectance.get_default_reflectance() # 2 - make an instance
reflectance.get_left() # 3 - use it
A comment can also sit on a line of its own, and either way you can write whatever
you like after the # — Python never looks at it. They cost nothing to run, so use
them freely: a line explaining why you picked a number is worth far more later
than one restating what the code plainly does.
And the rangefinder, later on:
from XRPLib.rangefinder import Rangefinder # 1 - import the class
rangefinder = Rangefinder.get_default_rangefinder() # 2 - make an instance
rangefinder.distance() # 3 - use it
Line them up beside the drivetrain and only the names differ. That's the whole trick: when a new device shows up, you already know two thirds of the code before anyone explains it.
Both of those are squashed into three lines so you can compare them. In a real program the first two lines of each would be up at the top with all the other setup, and the third would be down wherever the robot actually needs to read that sensor — quite possibly inside a loop, running hundreds of times.
A program starts with "from XRPLib.rangefinder import Rangefinder" and then calls rangefinder.distance(). It fails with a NameError. What was skipped?
Where do the three steps actually go in a real program?
The five syntax rules
Blockly never let you make a spelling mistake. Python will, so five rules matter. Here are the three you need today:
- Lowercase —
printis notPrint. Names are case-sensitive. - Parentheses — they're what tells Python "this name is a function call —
go run it."
printon its own just names the function;print()runs it. Inside the parentheses go the arguments: the values you're handing the function to work with. Some functions need one (print("hello")), some need several (drivetrain.straight(30)), and some need none at all — but the parentheses go in either way, empty or not. - Quotes — text goes in quotes:
print("hello").
The other two are about writing code that spans several lines, and every program in this lesson is a straight list of one-line instructions. You'll meet them in Lesson 1-10, at the moment you need them.
Which line is written correctly?
What do the parentheses in board.wait_for_button() do?
Activity · Write your first Python program
Same tool, different file. Python lives in XRP Code, exactly where your
blocks did — File → New File, then choose MICROPYTHON instead of Blockly.
The window is the one you already know: your robot's files down the left, the
editor in the middle, and the shell along the bottom, which is where
print() output shows up.
Create a file called lesson_1_8_first_drive.py — the lesson number, then what
the program does — and type this in. Typing, not pasting; it builds muscle memory.
Then press RUN:
from XRPLib.differential_drive import DifferentialDrive
from XRPLib.board import Board
drivetrain = DifferentialDrive.get_default_differential_drive()
board = Board.get_default_board()
board.wait_for_button()
print("Driving forward...")
drivetrain.straight(30)
print("Driving backward...")
drivetrain.straight(-30)
print("Done!")
board.wait_for_button() is the Wait for button press block you used all
through Blockly, in text. It matters more here than it looks: pressing RUN starts
the program immediately, so without that line the robot drives off the moment you
upload it — usually off the desk. Put the robot on the floor, press RUN, then
press the button on the robot.
Watch the shell at the bottom: your print() messages appear there as the robot
runs, so you can see exactly where it is in the program.
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)
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:
Machine learning
Most AI research and tools — including large language models — are built in Python.
Data & discovery
Scientists analyze experiments, genomes, and telescopes' data with Python every day.
Real robots
From classroom bots to industrial arms, Python is a standard language for robot control.
Wrap-up
- Why learn Python if Blockly works? (It's general-purpose rather than limited to the blocks provided, it's used everywhere from web development to data science to AI, and it lets you build on libraries other people have already written and tested.)
- Name a job Python does outside robotics. (Web development, data science, machine learning, automation — any of these.)
- Name the three syntax rules from this lesson. (Lowercase, parentheses, quotes — colons and indentation are still to come.)
- What do the parentheses in
print()tell Python? (That the name is a function call — run it; anything inside them is an argument.) - What is
print()good for? (Showing messages and debugging.)
Resources
- XRPLib Documentation
- XRP Code IDE — where you write both blocks and Python
- Python.org docs