Interactive Python

One of the great features of Python is the interactive interpreter. If you run the python3 command with no arguments it starts in an interactive mode in which you can enter Python code. The interpreter executes the code immediately and displays any calculated result.

Computer programmers call an interactive interpreter a “REPL”, which stands for Read/Eval/Print Loop. The interpreter reads some code from user input, evaluates the code, prints the result and loops round to repeat the process.

When programming in Python I find it useful to keep three windows open at all times: my text editor; a shell command prompt for running my program, tests or shell commands; and an interpreter prompt so that I can experiment with unfamiliar APIs, double-check my understanding of language features, run little manual or automated tests and use Python’s built-in help browser.

ipython3-in-action

The help browser is another great Python feature. The built-in help function can be used to read documentation about the modules, classes & functions of the Python APIs. We’ve written documentation for the Quick2Wire APIs that you can read with the help command. For example, the following command will show you the help for our GPIO API.

help('quick2wire.gpio')

IPython

The standard python3 interpreter has a pretty rudimentary REPL. There’s a much better one in the Raspbian software repository called ipython3, which provides a lot of help for entering and working with Python code. It auto-indents multi-line control flow statements, function and class definitions. It can auto-complete names for you: pressing Tab will complete a partially entered name from those that are in scope. It has a searchable command history that persists between sessions, so you can easily repeat or edit previous input. It can reload modules that you have imported, so if you change a code in your editor you can reload it into thee REPL. And much much more.

You can install ipython3 with the command:

sudo apt-get install ipython3

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>