We continue looking at the basic syntax of MicroPython. In this tutorial, we will see how to work with variables and data types.
Although MicroPython is a reduced version of Python, it basically shares the same types and way of working with variables as its “big brother”.
So let’s review it briefly. When there is an important difference between MicroPython and Python, I will point it out.
What are variables?
A variable is a space in memory that stores a value that can change during the execution of a program.
In Python and MicroPython, variables are declared dynamically, and their type is automatically inferred based on the value assigned to it (meaning it’s not necessary to specify its type).
my_variable = 10
In this case,
my_variableis a variable that stores the value10.- MicroPython infers that the type of
my_variableis an integer (int).
Variable Reassignment
One of the characteristics of Python (and shared by MicroPython) is that variables can change type during program execution.
my_variable = 10 # Now it is an integer
my_variable = "Hello" # Now it is a string
This is possible because Python is a dynamically typed language. That is, the type of variables is not fixed.
However, in MicroPython, where resources are limited, it is recommended to avoid unnecessary type changes to optimize memory usage.
Data Types in MicroPython
MicroPython supports the basic Python data types, although with some limitations due to hardware constraints.
Next, we will see the most common data types and how they are used.
| Data Type | Description | Example |
|---|---|---|
Integers (int) | Numbers without decimals | 42 |
Floats (float) | Numbers with decimals | 3.14 |
Strings (str) | Sequences of characters | "Hello, MicroPython" |
Booleans (bool) | Truth values (True or False) | True / False |
Integers (int)
Integers are numbers without a decimal part. In MicroPython, integers can be of arbitrary size. But in practice, they are much more limited by the available memory (which is much less than on a PC).
integer = 42
Let’s see the operations we can do with integers
a = 10
b = 3
sum = a + b # 13
subtraction = a - b # 7
multiplication = a * b # 30
division = a / b # 3.333... (in Python 3, division always returns a float)
integer_division = a // b # 3 (integer division)
modulus = a % b # 1 (remainder of the division)
Floating-point numbers
Floating-point numbers are numbers with a decimal part. In MicroPython, floats are usually 32-bit (in Python they are usually 64-bit), which means they have limited precision.
floating = 3.14
The operations we can do with floats are the usual ones 😆
a = 5.0
b = 2.0
sum = a + b # 7.0
subtraction = a - b # 3.0
multiplication = a * b # 10.0
division = a / b # 2.5
Strings
Strings are sequences of characters. Just like in Python, in MicroPython strings can be defined using single quotes (') or double quotes (").
string = "Hello, MicroPython"
Let’s review some common string operations
greeting = "Hello"
name = "MicroPython"
message = greeting + ", " + name # "Hello, MicroPython"
length = len(message) # 16
substring = message[0:4] # "Hell"
Booleans
Booleans are a data type that can only have two values: True or False.
is_true = True
is_false = False
They are useful for expressing conditions and making decisions in code. Common operations we can perform with booleans are,
a = True
b = False
logical_and = a and b # False
logical_or = a or b # True
logical_not = not a # False
Collections
Another strong point of Python is its dynamic collections included by default. MicroPython incorporates practically all the collections and features.
Let’s look at some of them.
Lists
Lists are ordered and mutable collections of elements. In both Python and MicroPython, lists are one of the most beloved and used data structures due to their flexibility.
my_list = [1, 2, 3, 4, 5]
Some common list operations
my_list = [1, 2, 3]
my_list.append(4) # [1, 2, 3, 4]
my_list[0] = 10 # [10, 2, 3, 4]
length = len(my_list) # 4
Tuples
Tuples are similar to lists, but they are immutable, meaning they cannot be modified after creation. They are useful for storing data that should not change.
my_tuple = (1, 2, 3)
Common tuple operations are,
my_tuple = (1, 2, 3)
first_element = my_tuple[0] # 1
length = len(my_tuple) # 3
Dictionaries
Dictionaries are collections of key-value pairs. They are useful for storing and retrieving data efficiently using a unique key.
my_dictionary = {"key1": "value1", "key2": "value2"}
And finally, some common dictionary operations
my_dictionary = {"name": "MicroPython", "version": 1.0}
value = my_dictionary["name"] # "MicroPython"
my_dictionary["version"] = 1.1 # Updates the value
my_dictionary["new"] = "value" # Adds a new key-value pair
So, basically we have the same types as in Python. We just need to be aware that we have much less memory, so we can’t do as many “crazy things”.
