micropython-sintaxis-basica

Basic Syntax of MicroPython

  • 4 min

Now that we have everything ready 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, which is one of the reasons it has gained its popularity among both beginners and experienced developers.

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-limited devices).

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

Learn the basics of programming with the Python course

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

Significant Indentation

One of Python’s characteristics 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 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
Copied!

Comments in MicroPython

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

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
Copied!

Triple quotes (""" or ''') can be used.

"""
This is a multi-line comment.
"""
print("MicroPython in action")
Copied!

Variables and Data Types

In Python, variables are used to store data. It is not necessary to declare the type of a variable explicitly, 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
Copied!

Operators

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

Operator TypeOperators
Arithmetic+, -, *, /, %
Comparison==, !=, <, >, <=, >=
Logicaland, or, not
x = 10
y = 5
sum_result = x + y  # Sum
is_greater = x > y  # Comparison
Copied!

Conditionals and Loops

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

Functions

Functions are reusable blocks of code that perform a specific task (or at least they should 😜).

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

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

greet("MicroPython")
Copied!

Exception Handling

Exception handling allows managing errors that may occur during program execution. 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")
Copied!

Modules and Libraries

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