ESP8266 MicroPython Tutorial: 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.
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  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 .
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 . So, in a dynamic language, a variable is a name bound to a value / object . 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 . We don’t specify that the variable name belongs to a certain type because it can change during the execution of the program.
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.
1 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:
1 int x = 1;
Now, let’s assign a string to that same x variable.
1 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.
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.
1 stringVar = "Hello"
2 integerVar = 10
Now, try to add both variables, as indicated bellow.
1 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.
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.
1 print (stringVar + str(integerVar))
You can check in figure 3 that now it is possible to perform the operation.
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.
1 stringVar = "1"
2 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.
Figure 4 – String to integer conversion.
NOTE: This article is written by Nuno Santos who is an kindly Electronics and Computers Engineer. live in Lisbon, Portugal. you could check the original article here.
He had written many useful tutorials and projects about ESP32, ESP8266, If you are interested, you could check his blog to know more.