Basics

1. Indentation

Indentation means adding spaces at the beginning of a line of code. Python uses indentation to identify which instructions belong together.

Use 4 spaces for each indentation level.

if True:
print("Hello Robot!")
print("Welcome to MicroPython!")

Remember:

  • Instructions inside a block must have the same indentation.
  • Use a colon : after statements such as if, while, for, and def.
  • Incorrect indentation can cause an error.

2. Comments

Comments are notes written inside your program to explain what the code does.

Use the # symbol to write a comment.

# This is a comment
# Turn on the LEDprint("LED ON")
# This line explains the program

Comments are ignored by Python and are not executed.

3. print() – Serial Port

The print() function displays messages, numbers, and variable values in the serial console.

It is useful for checking what your program is doing and displaying sensor readings.

print("Hello World!")
print("Welcome to Clu-Bots")
temperature = 25
print(temperature)

You can also display multiple values:

name = "Robot"
speed = 100
print("Name:", name)
print("Speed:", speed)

Remember: print() displays information on the serial console; it does not send text to an LCD or OLED display.

4. Variables

Variables are used to store information that your program can use later.

You can think of a variable as a small box that holds a value.

name = "Clu-Bot"
age = 11
speed = 100
temperature = 25.5

Using variables:

robot_name = "Clu-Bot"
motor_speed = 80
print(robot_name)
print(motor_speed)

Rules:

  • Variable names cannot contain spaces.
  • They cannot start with a number.
  • Use meaningful names to make your code easier to understand.