MicroPython ‘boolean’ Data Type

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 Python:

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

Boolean Definition and Properties

The boolean data type can hold only one of two value: True or False.

Computer programs often need to evaluate an expression to determine if it is true or false. The result is used as a decision point to repeat an action or branch elsewhere in the code, etc. A comparison operation on two values will always return either True or False.

Example 1

# A series of statements that return a boolean value

'A' < 'B'  ⇒ True
5 == 5.0  ⇒ True
(14/2) > (2+6)  ⇒ False

# MicroPython dictionaries are not ordered.
dict1 = {'Color': 'red', 'Doors': 4}
dict2 = {'Doors' : 4, 'Color' : 'red'}
dict1 == dict2  ⇒ True

# MicroPython lists are ordered.
list1 = [0, 1, 2]
list2 = [2, 1, 0]
list1 == list2  ⇒ False

          

Function bool()

The function bool() performs a boolean evaluation on a value or variable and returns either True or False.

bool(value) = True

Most values when passed to bool() return True. The following examples all return True:


bool(5)  ⇒ True
bool({'Color' : 'red', 'Doors' : 4})  ⇒ True
bool('Spam')  ⇒ True
bool(True + False)  ⇒ True
          

The last example is an interesting one. MicroPython will evaluate (True + False) as (1 + 0)  i.e.  bool(1) so returns True.

bool(value) = False

The following values passed to bool() all evaluate to False:

  1. The value zero 0
  2. The value False
  3. Empty strings ""
  4. Any list, tuple, set, and dictionary that is empty () [] {}

The following are all False:


bool(0)  ⇒ False
bool(None)  ⇒ False
bool('')  ⇒ False
bool({})  ⇒ False
          

User Defined Boolean Functions

Functions can also return a boolean value. This can assist to make code more readable.

Example 2


# Demonstrates the use of a user defined function
# that has a boolean return type generated with the
# builtin function bool().

# User defined function returns True
# if 'num' is an even number.
def is_even(num):
    # By definition bool(0) returns False
    return not(bool(num % 2))

# Define a list   
list1 = [-5, 24, 'Spam', 0]

# Iterate through all elements in the list.
# Firstly check that each item is numerical.
# Then if it is a number determine whether
# it is even or odd.
for item in list1:
    if (type(item) == int or type(item) == float):
        if (is_even(item)):
           print(item, 'is an even number')
        else:
           print(item, 'is an odd number') 
    else:
        print(item, 'is not numeric')
        
print('\nProgram ending...')

          

-5 is an odd number
24 is an even number
Spam is not numeric
0 is an even number

Program ending...
          

The function is_even() accepts a single number as its parameter. The number is divided by 2 and if the remainder is zero (0) then True is returned else False. This relies on bool(0) returning False, i.e no remainder, returning a False value which is converted to True by the not operator.