ESP8266 MicroPython: Simple variables

The objective of this post is to explain how to declare and handle simple variables in MicroPython, with the ESP8266.


The objective of this post is to explain how to declare and handle simple variables in MicroPython, with the ESP8266. First of all, we need to take in consideration that Python is both a strongly typed and dynamic typed language [1].

Being a strongly typed language means that the type of a value doesn’t change without an explicit type conversion. So, we cannot perform operations that are not allowed for a certain data type [1] and the interpreter / compiler will not do an implicit conversion of the data types. So, for example, in Python, we cannot add a number to a string because it will raise an error [1].

For example, to add a number to a string in Python, we either explicitly convert the string to a integer and perform a arithmetic operation, or convert the integer to a string and perform a string concatenation operation.

Nevertheless, this should not be confused with dynamic typing, which is other characteristic of Python [1]. So, in a dynamic language, a variable is a name bound to a value / object [1][2]. But the name itself doesn’t have a type, the value does.

So, at a certain moment, a variable can hold a number and latter a string [1]. We don’t specify that the variable name belongs to a certain type because it can change during the execution of the program.

Coding examples

If you need help getting started with MicroPython on the ESP8266, please check this previous post. After configuring the environment, connect to the MicroPython prompt.

We will start by declaring a variable and assigning an integer to it. To do so, just type the following command and hit enter.

x = 1

Note that, as stated in the introduction, we don’t specify the type of the variable. This is more flexible than, for example, in Arduino, where we would need the following code for the same assignment:

int x = 1;

Now, let’s assign a string to that same x variable.

x = "Hello from MicroPython"

As indicated in figure 1, this works just fine because Python is dynamically typed. If we did something similar on Arduino, we would get an error, since the variable name x was already bounded to an integer type.

ESP8266 MicroPython variable assignment

Figure 1 – Variable assignment in MicroPython.

Now, let’s confirm that the language is also strongly typed. To do so, we declare two variables, an integer and a string.

stringVar = "Hello"
integerVar = 10

Now, try to add both variables, as indicated bellow.

stringVar + integerVar

You should get the error indicated in figure 2, because we are trying to add an integer to a string, which doesn’t make sense since the types are different. Also, there is no implicit type conversion.

ESP8266 MicroPython add string to integer error

Figure 2 – Error in adding string to integer.

So, if we want to add these two variables, we need to convert the integer value to a string, in order for a concatenation operation to be possible. To perform this conversion, we can use the str function.

print (stringVar + str(integerVar))

You can check in figure 3 that now it is possible to perform the operation.

ESP8266 MicroPython int to string conversion

Figure 3 – Integer to string conversion.

We can also perform a string to integer conversion and do a sum between the variables. To do so we use the int function. Naturally, the string to be converted should hold a number.

stringVar = "1"
print (int(stringVar) + integerVar)

As can be seen in figure 4, we get the arithmetic sum of the variables. Remember that the integerVar had the value 10.

ESP8266 MicroPython string to int conversion

Figure 4 – String to integer conversion.

Related Posts


Related content





Technical details

MicroPython version: v1.8.7

Leave a Reply