IPython

An enhanced interactive Python interpreter

Author

Clemens Brunner

Published

June 29, 2026

Introduction

Running uv run python (without any further arguments) from a command line shell (such as Terminal on macOS or Windows Terminal on Windows) invokes the interactive Python interpreter. This interpreter mode is especially useful for interactively playing around with data using a workflow called the read–eval–print loop (REPL), in which Python reads a command (which the user just entered), evaluates it, prints its result, and repeats these steps for every subsequent command in a loop.

Here’s an example. After starting the interactive Python interpreter, we are presented with a prompt that looks like this:

>>>

The prompt indicates that Python is waiting for us to type a command. After entering 1 + 1 and confirming with ⏎, Python evaluates the statement and prints its result on the next line:

>>> 1 + 1
2

All of this is already built into the standard Python interpreter. However, the interactive experience is pretty bare-bones: there is no syntax highlighting, tab completion is rudimentary, there is no shortcut for viewing documentation, printing or changing the working directory requires importing the os module, and quickly running a shell command (such as ls to list the contents of the working directory) is not possible. All of these things and more are supported in IPython (see this page for a summary of the most important differences between standard Python and IPython).

First steps

If you are using uv to manage your Python projects, you can simply start IPython in your project directory by running the following command in the terminal:

uv run --with ipython ipython

The prompt looks a little different from the one in the standard Python interpreter:

In [1]:

It includes a counter in square brackets, so we always know how many commands we have already entered in the current session. Let’s try the same command we used in the previous section:

In [1]: 1 + 1
Out[1]: 2

Every output is also numbered, which makes it really easy to connect it to the corresponding input. In addition, the prompts are colored (which unfortunately cannot be reproduced here).

IPython also creates names for recent expressions. For example, we can access the last result with an underscore _:

In [2]: _
Out[2]: 2

In addition to the last result, we can also retrieve the two previous ones with double and triple underscores, respectively. Alternatively, we can access a specific output by name via _1, _2, etc. (the numbers correspond to the output number). This also works for inputs using _i1, _i2, and so on.

Finally, all inputs and outputs are also available as In (a list) and Out (a dictionary). For example, the first input and output can be accessed with In[1] and Out[1], respectively.

Keyboard shortcuts

IPython offers many useful keyboard shortcuts that can significantly speed up an interactive development workflow. The most important shortcuts are:

  • ↑ and ↓ go back and forward in the command history. This even works when first typing a few characters and then pressing the up arrow key, which will search only those commands that start with the given characters.
  • Ctrlr starts a reverse search for a command. For example, if you reverse-search for “sum”, you will get the first (most recent) command in the history which contains these characters. Repeatedly pressing Ctrlr searches previous matching commands.
  • ? followed by a Python name displays the corresponding documentation. For example, ?sum shows the documentation for the built-in sum() function. It is also possible to append ? to the name, such as sum?.
  • Tab completion works with most Python objects. For example, if we want to know which methods are available for str, we type str. and then hit the Tab key. This brings up a list of all possible methods associated with str, which we can navigate either with Tab or the arrow keys.
  • Ctrlc deletes the current input and also interrupts any running operation.
  • Ctrll clears the screen.
Note

Some of these shortcuts are also available in the regular interactive Python interpreter.

Magic commands

Magic commands are special IPython commands that provide additional functionality such as printing or changing the working directory. Normally, magic commands start with a % character, but by default they are also available without this prefix (unless a Python object with the same name exists). Here are some frequently used magic commands (a full list is available here):

  • %cd <some_directory> changes the working directory to <some_directory>.
  • %pwd prints the current working directory.
  • %ls lists the contents of the current working directory.
  • %history prints the (input) history.
  • %matplotlib enables interactive Matplotlib plotting (which means that plots are immediately shown and do not block the REPL).
  • %paste pastes text (code) from the clipboard.
  • %pycat <file.txt> displays the contents of the file <file.txt> with syntax highlighting.
  • %run <script.py> runs the entire script <script.py> in the current interactive session.
  • %timeit <statement> times the execution of a statement.
Important

Magic commands are not valid Python, so use them only in the interactive interpreter – do not use them in Python scripts!

Shell commands

It is often useful to run commands directly in the underlying shell. This can be achieved in IPython by prefixing a shell command with a ! (bang). Here are some examples that work on macOS and Linux (but probably not on Windows, which in general uses different shell commands):

  • !ls lists the contents of the working directory.
  • !less <some_file.txt> shows the contents of the file <some_file.txt>.
  • !date runs the date shell command (which displays the current time and date).

It is even possible to capture the output of a shell command to access it in Python later on, for example:

d = !date
d
['Wed Jul  1 08:36:05 CEST 2026']
files = !ls
files[:5]
['_publish.yml',
 '\x1b[34m01-ipython_files\x1b[m\x1b[m',
 '01-ipython.html',
 '01-ipython.qmd',
 '01-ipython.quarto_ipynb']
Tip

Running shell commands only works for a given shell. For example, the date command is available on most macOS and Linux shells, but not on Windows (which has the Get-Date command instead).

Therefore, always check if there are suitable magic commands available before using shell commands. For example, listing the contents of the working directory can be achieved with %ls, and showing the contents of a file can be done with %pycat. These magic commands are cross-platform and work regardless of the underlying shell or operating system.

Jupyter notebooks

IPython includes support for web-based documents for literate programming, so-called Jupyter notebooks. They combine Markdown text with code cells, which is useful for exploratory data analysis, tutorials, lecture notes, and so on. However, they are not great for reproducible research (unless the whole notebook is rendered from top to bottom and all results are computed from scratch).

Quarto builds upon Jupyter notebooks (and many other tools) to provide a scientific and technical publishing system, which can not only render to HTML, but also to PDF, Word, Reveal.js, and more. Because Quarto documents run all code cells during rendering, this format is better suited for reproducible documents.

Note

Quarto and Jupyter support languages other than Python, for example R and Julia.

We will not cover any of these notebook-like tools in this course, but it is good to know that they exist (so you know where to start). By the way, the course material you are reading right now is written with Quarto, and if you are interested, you can take a look at the source to find out how it works.


© Clemens Brunner (CC BY-NC-SA 4.0)