In MicroPython a variable is created by plain assignment, without declaring its type: x = 5 is enough. This is dynamic typing: the interpreter works out the type, and one and the same variable can even change type along the way. That is the big difference with C++ (int x = 5;), a source of concision… and of one pitfall worth knowing. This page covers the basic types and the essential containers.
No declaration, a deduced type
temperature = 21.5 # floatnom = "Pico" # str (character string)actif = True # boolnombre = 42 # int
The type can change silently. Since nothing is declared, reassigning x = "text" after x = 5 is allowed, and it can introduce a bug that is hard to spot. Keep a variable on a single type unless there is a clear reason not to.
/ is not //.5 / 2 gives 2.5 (a float), not 2. For an integer division, use //. It is the opposite of the C reflex, where / between integers truncates.
The Pico computes in single precision. Its float values sit on 32 bits, not 64 as on a computer, so a perfectly simple calculation can display stray decimals. float("3.3") * 2 may give 6.5999999 instead of 6.6. That is normal. The consequence: do not compare two floats for strict equality (==), but test a small gap (abs(a - b) < 0.001).
str + int raises an error."value: " + 42 raises a TypeError, so you have to convert (+ str(42)). Python does not mix types implicitly.
Memory cost. A Python object is heavier than a C integer. That does not matter while learning, but it is worth bearing in mind on large arrays (see memory management).
Exercises
Exercise 1 — Inspect and convert
At the REPL, create a variable holding the string "3.3", check its type, then convert it into a float and multiply it by 2.
Solution
>>> v = "3.3">>> type(v)<class 'str'>>>> float(v) * 26.6
The string "3.3" is not a number until you convert it: float(v) turns it into a float, which you can then compute with. Multiplying the string (v * 2) would give '3.33.3', hence the importance of the conversion. On the Pico (single precision), the displayed result may be 6.5999999 rather than 6.6: the calculation is right, it is the display that reveals the limit (see the pitfall).
Exercise 2 — Average of some readings
From a list of three readings [512, 530, 528], work out their average. Hint: sum() and len().
sum() adds the elements up, len() counts them. True division / gives an average as a float. You will meet this pattern again when smoothing noisy readings.