A

MicroPython Reference

This appendix provides a reference for some of the key features and functions available in MicroPython. Treat it as a reference resource, and try out some of the functions to see how they work. The REPL is your friend. Try things out. For full details on everything in Python, go to https://microbit-micropython.readthedocs.io/en/latest/.

Language Contructs

Table A-1 shows some of the functions you can use with numbers.

Table A-1   Language Constructs

Images

Images

Comparisons

To test to see whether two values are the same, we use ==. This is called a comparison operator. The comparison operators that we can use are shown in Table A-2.

Table A-2   Comparisons

Images

Numbers

Table A-3 shows some of the functions you can use with numbers.

Table A-3   Number Functions

Images

Strings

String constants can be enclosed either with single quotes (most common) or with double quotes. Double quotes are useful if you want to include single quotes in the string like this:

Images

There are other occasions when you may want to include special characters such as end-of-lines or tabs in a string. To do this, you use what is called escape characters that begin with a backslash (\). The only ones that you are likely to need are

Images   \t—tab character

Images   \n—new line character

Table A-4 lists some of the strings functions that may be useful.

Table A-4   String Functions

Images

Lists

Table A-5 summarizes the common List functions.

Table A-5   List Functions

Images

Dictionaries

Table A-6 lists some common Dictionary functions.

Table A-6   Dictionary Functions

Images

Type Conversions

Python contains a number of built-in functions for converting things of one type to another. These are listed in Table A-7.

Table A-7   Type Conversions

Images