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

Arithmetic Operators

All seven arithmetic operators available in Python are also found in MicroPython.

Table 1: MicroPython (micro:bit) - Arithmetic Operators
Op Name Example
- Subtraction[1] (12 - 5) ⇒ 7
+ Addition (7 + 3) ⇒ 10
* Multiplication (7 * 3) ⇒ 21
/ Division[2] (7 / 3) ⇒ 2.333333
% Modulus Returns the remainder of a division

(7 % 3) ⇒ 1
(-7 % 3) ⇒ 2

** Exponentiation (7 ** 3) ⇒ 343
7 * 7 * 7 ⇒ 343

Alternative:
pow(7, 3) ⇒ 343

// Floor Division[3] (7 // 3) ⇒ 2
(-7 // 3) ⇒ -3
(-7 // -3) ⇒ 2