micropython-sintaxis-basica

Basic Syntax of MicroPython

  • 3 min

Now that we have everything prepared to start, let’s begin by looking at the basic syntax of Python applied to MicroPython.

As we know, Python is known for its simple and readable syntax, and it is one of the reasons that has contributed to its popularity among both beginners and experienced users.

Although MicroPython is a reduced version of Python, it retains most of the essential features of the language (with some adaptations to work on resource-constrained devices).

So let’s briefly review some of the key elements of Python’s basic syntax that are applicable in MicroPython. If you need more information about Python, I leave you a course.

👆 If you want to know more, check out the Python course

Significant Indentation

One of the features of Python is that it uses significant indentation (spaces or tabs at the beginning of a line) to define code blocks.

That is, unlike other languages that use curly braces {} or begin/end, in Python indentation is mandatory and defines the structure of the code.

if True:
    print("This is correctly indented")  # Correct

# print("This is not correctly indented")  # This would cause an error

Comments in MicroPython

Comments allow you to add notes in the code without affecting its execution. They are useful for documenting the code and making it easier to understand.

  • Single-line comment: The # symbol is used.
# This is a single-line comment
print("Hello, MicroPython")  # It can also be used at the end of a line of code
  • Multi-line comment: Triple quotes (""" or ''') can be used.
"""
This is a multi-line comment.
"""
print("MicroPython in action")

Variables and Data Types

In Python, variables are used to store data. It is not necessary to explicitly declare the type of a variable, as Python is a dynamically typed language.

MicroPython follows exactly the same convention and operation. So defining variables is as simple as doing,

# Variable declaration
integer = 42
float_number = 3.14
string = "Hello, MicroPython"
boolean = True

Operators

Operators allow us to perform operations with data. MicroPython shares the same operators as Python. Some of the most common are,

Type of OperatorOperators
Arithmetic+, -, *, /, %
Comparison==, !=, <, >, <=, >=
Logicaland, or, not
x = 10
y = 5
sum_result = x + y  # Sum
is_greater = x > y  # Comparison

Conditionals and Loops

Conditionals and loops allow us to modify the flow of execution of the program. In Python, the most common control structures are if, else, for, and while. These structures are equally valid in MicroPython.

Functions

Functions are reusable code blocks that perform a specific task (or should do it 😜).

In MicroPython and Python, functions are defined using the def keyword.

def greet(name):
    print("Hello,", name)

greet("MicroPython")

Exception Handling

Exception handling allows you to manage errors that may occur during the execution of the program. In Python, this is done using the try, except, finally blocks.

try:
    result = 10 / 0
except ZeroDivisionError:
    print("Error: Division by zero")
finally:
    print("This block always executes")

Modules and Libraries

In Python, modules and libraries allow you to extend the functionality of the language. MicroPython includes a selection of standard modules, but we can also create our own or use others developed by the community.