MicroPython Identity Operators

Contents

Introduction

Operators are an essential part of just about any computing language ever devised and MicroPython is of no exception. The MicroPython operators can be broadly classified into eight groups:

  1. Arithmetic
    - , + , * , / , % , ** , //
  2. Assignment
    = , augmented assignments e.g. +=
  3. Comparison
    == , != , > , < , >= , <=
  4. Logical
    and , or , not
  5. Identity
    is , is not
  6. Membership
    in , not in
  7. Bitwise
    & , ! , ^ , ~ , << , >>
  8. Unpacking Operators
    * , **

Identity Operators

There are only two MicroPython identity operators. Though these identity operators won't often be used for simple project it is still useful to understand how they work. They provide a useful insight into how MicroPython creates objects (variables) and associates memory to these objects to hold data values.

Table 1: MicroPython Identity Operators
Operator Description
is[1]

Tests if two variables refer to the same object.

x = ['cat', 'dog']
y = ['cat', 'dog']
z = x

(x is y) ⇒ False
(x == y) ⇒ True
(x is z) ⇒ True
(x == z) ⇒ True

is not

Tests if two variables don't refer to the same object

x = ['cat', 'dog']
y = ['cat', 'dog']
z = x

(x is not y) ⇒ True
(x != y) ⇒ False
(x is not z) ⇒ False
(x != z) ⇒ False

The above examples use Python lists. If the reader is not familiar with this data type then a tutorial can be found here