This section covers some characteristics of the MicroPython Interactive
Interpreter Mode. A commonly used term for this is REPL (read-eval-print-loop)
which will be used to refer to this interactive prompt.

When typing python statements which end in a colon (for example if, for, while)
then the prompt will change to three dots (...) and the cursor will be indented
by 4 spaces. When you press return, the next line will continue at the same
level of indentation for regular statements or an additional level of indentation
where appropriate. If you press the backspace key then it will undo one
level of indentation.

If your cursor is all the way back at the beginning, pressing RETURN will then
execute the code that you’ve entered. The following shows what you’d see
after entering a for statement (the underscore shows where the cursor winds up):

>>> foriinrange(3):... _

If you then enter an if statement, an additional level of indentation will be
provided:

Auto-indent won’t be applied if the previous two lines were all spaces. This
means that you can finish entering a compound statement by pressing RETURN
twice, and then a third press will finish and execute.

While typing a command at the REPL, if the line typed so far corresponds to
the beginning of the name of something, then pressing TAB will show
possible things that could be entered. For example type m and press TAB
and it should expand to machine. Enter a dot . and press TAB again. You
should see something like:

The word will be expanded as much as possible until multiple possibilities exist.
For example, type machine.Pin.AF3 and press TAB and it will expand to
machine.Pin.AF3_TIM. Pressing TAB a second time will show the possible
expansions:

You can interrupt a running program by pressing Ctrl-C. This will raise a KeyboardInterrupt
which will bring you back to the REPL, providing your program doesn’t intercept the
KeyboardInterrupt exception.

If you want to paste some code into your terminal window, the auto-indent feature
will mess things up. For example, if you had the following python code:

deffoo():print('This is a test to show paste mode')print('Here is a second line')foo()

and you try to paste this into the normal REPL, then you will see something like
this:

>>> deffoo():... print('This is a test to show paste mode')... print('Here is a second line')... foo()...
File "<stdin>", line 3IndentationError: unexpected indent

If you press Ctrl-E, then you will enter paste mode, which essentially turns off
the auto-indent feature, and changes the prompt from >>> to ===. For example:

>>>paste mode; Ctrl-C to cancel, Ctrl-D to finish=== def foo():=== print('This is a test to show paste mode')=== print('Here is a second line')=== foo()===This is a test to show paste modeHere is a second line>>>

Paste Mode allows blank lines to be pasted. The pasted text is compiled as if
it were a file. Pressing Ctrl-D exits paste mode and initiates the compilation.

When you use the REPL, you may perform computations and see the results.
MicroPython stores the results of the previous statement in the variable _ (underscore).
So you can use the underscore to save the result in a variable. For example:

Raw mode is not something that a person would normally use. It is intended for
programmatic use. It essentially behaves like paste mode with echo turned off.

Raw mode is entered using Ctrl-A. You then send your python code, followed by
a Ctrl-D. The Ctrl-D will be acknowledged by ‘OK’ and then the python code will
be compiled and executed. Any output (or errors) will be sent back. Entering
Ctrl-B will leave raw mode and return the the regular (aka friendly) REPL.

The tools/pyboard.py program uses the raw REPL to execute python files on the
MicroPython board.