MicroPython Numeric Data Types

Contents

Introduction

MicroPython is a “slim” version of Python specifically designed with a small footprint to efficiently run on memory constrained microcontrollers.

Computer languages have a built-in set of data types along with user capability to define custom types. MicroPython is no exception. Following are the standard or built-in data types of MicroPython:

  • Numeric
  • Boolean
  • Sequence Type
  • Set
  • Dictionary
Python data type hierarchy
Source: geeksforgeeks.org

As shown by this diagram, Python's built-in numeric data types can be categorised into the subtypes:

  • Integer
  • Complex Number
  • Float

The complex number type is of limited use for beginner microcontroller applications and will not be discussed further[1].

Integer

This value is represented by the int class. It contains positive or negative whole numbers (i.e. without fraction or decimal). In MicroPython there is no limit to how long an integer value can be; only being limited by machine memory.

Other languages further break integers down e.g. short integers, positive only integers, long integers and so on. MicroPython does not do this; i.e. int is simply int.

Python provides the built-in function type(). The following program demonstrates MicroPython indeed has only one type of integer (int). Copy the code from this webpage to the Mu Editor, Save and Flash to the micro:bit. Click the REPL button to see the program's output.

Example 1


print("12345 ; type is", type(12345))
print("-12345 ; type is", type(-12345))
print("123456789098765432123456789 ; type is",
       type(123456789098765432123456789))
          

Output:


12345 ; type is <class 'int'>
-12345 ; type is <class 'int'>
123456789098765432123456789 ; type is <class 'int'>
          

Float

This value is represented by the float class. It is a real number with floating point representation. It is specified by a decimal point. Optionally, the character e or E followed by a positive or negative integer may be appended to specify scientific notation; e.g. 245.0 can be represented as 2.45E2

Try the following program in the micro:bit using the Mu Editor:

Example 2


print("12345.98 ; type is", type(12345.98))
print("-12345.98 ; type is", type(-12345.98))
print("12. ; type is", type(12.))
print("23e2 ; type is", type(23e2))
print("5.97E-200 ; type is", type(5.97E-200))
          

Output:


12345.98 ; type is <class 'float'>
-12345.98 ; type is <class 'float'>
12. ; type is <class 'float'>
23e2 ; type is <class 'float'>
5.97E-200 ; type is <class 'float'>

Type Conversions

There are two types of Type Conversion in MicroPython:

  1. Implicit Type Conversion
  2. Explicit Type Conversion

In Implicit Type Conversion the MicroPython interpreter automatically converts one data type to another without any user involvement. This occurs when an expression has a mixture of different types. For example, an expression with a mix of integer and float will produce a result of type float. This ensures no loss of information as float is the wider-sized type.

The following program demonstrates this concept.

Example 3


a = 5
b = 21.3
c = a + b
print("a =", a, " ; Type is:", type(a))
print("b =", b, " ; Type is:", type(b))
print("c =", c, " ; Type is:", type(c))
          

Output:


a = 5  ; Type is: <class 'int'>
b = 21.3  ; Type is: <class 'float'>
c = 26.3  ; Type is: <class 'float'>

In the above example the variable a is implicitly converted from int to float. The variable c becomes type float.

In an Explicit Type Conversion the data type is changed by the user. There is a risk of data loss such as when a float is explicitly changed to an integer.

⇒ The function int() converts other data types to integer.

⇒ The function float() converts other data types to float.

The following small program demonstrates this.

Example 4


a = 5.9
b = 21.3

c = int(a + b)
print("c =", c, "; type(c) is ", type(c))

d = int(a) + int(b)
print("d =", d, "; type(d) is ", type(d))

print("float(5) =", float(5))
          

Output:


c = 27 ; type(c) is  <class 'int'>
d = 26 ; type(d) is  <class 'int'>
float(5) = 5.0

          

Code Explanation

c = int(a + b)
  = int(5.9 + 21.3)
  = int(27.2)
  = 27

d = int(a) + int(b)
  = int(5.9) + int(21.3)
  = 5 + 21
  = 26
            

It's sometimes necessary to explicitly use int() or float() to prevent an error from occurring. Consider the following examples:


5 + '6'  ⇒ TypeError: unsupported types for __add__: 'int', 'str'
5 + int('6') ⇒ 11

3.4567 * '45.87' ⇒ TypeError: unsupported types for : 'float', 'str'
3.4567 * float('45.87') ⇒  158.558