MicroPython Membership 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
    * , **

Membership Operations

Membership operators are used to test if a sequence is presented in an object. It is a simple concept that is best understood by following the examples in the table below.

Membership Operators

MicroPython provides two membership operators Both are useful and well worth understanding.

Table 1: MicroPython Identity Operators
Operator Description
in

Returns True if a sequence of the specified value is present in the object.

x = ['apple', 'banana']
'banana' in x ⇒ True

not in

Returns True if a sequence with the specified value is not present in the object.

x = ['apple', 'banana']
'pineapple' not in x ⇒ True

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