SunFounder ESP32 Starter Kit
3.6 1.6 (Optional) MicroPython Basic Syntax
3.6.1 Indentation
Indentation refers to the spaces at the beginning of a code line. Like standard Python programs, MicroPython programs
usually run from top to bottom: It traverses each line in turn, runs it in the interpreter, and then continues to the next
line, Just like you type them line by line in the Shell. A program that just browses the instruction list line by line is
not very smart, though - so MicroPython, just like Python, has its own method to control the sequence of its program
execution: indentation.
You must put at least one space before print(), otherwise an error message “Invalid syntax” will appear. It is usually
recommended to standardise spaces by pressing the Tab key uniformly.
if 8 > 5:
print("Eight is greater than Five!")
>>> %Run -c $EDITOR_CONTENT
Traceback (most recent call last):
File "<stdin>", line 2
SyntaxError: invalid syntax
You must use the same number of spaces in the same block of code, or Python will give you an error.
if 8 > 5:
print("Eight is greater than Five!")
print("Eight is greater than Five")
>>> %Run -c $EDITOR_CONTENT
Traceback (most recent call last):
File "<stdin>", line 2
SyntaxError: invalid syntax
3.6.2 Comments
The comments in the code help us understand the code, make the entire code more readable and comment out part of
the code during testing, so that this part of the code does not run.
Single-line Comment
Single-line comments in MicroPython begin with #, and the following text is considered a comment until the end of
the line. Comments can be placed before or after the code.
print("hello world") #This is a annotationhello world
>>> %Run -c $EDITOR_CONTENT
hello world
Comments are not necessarily text used to explain the code. You can also comment out part of the code to prevent
micropython from running the code.
280 Chapter 3. For MicroPython User