Python

Any REPL can be used as a “shell”. The quickest way to use Python is with the built-in profile:

extensions:
  term:
    profile: python

This sets shell: python3, shell-args: ["-q"], prompt: ">>>", and ps2: "...". You can override any of these individually, for example to add strip-auto-indent: true for Python 3.13+ (PyREPL):

extensions:
  term:
    profile: python
    strip-auto-indent: true

Python 3.13+ includes PyREPL with syntax highlighting. The strip-auto-indent option counteracts PyREPL’s automatic indentation of continuation lines. Wrap >>> and ... in backticks since pandoc interprets bare > as a blockquote marker.

Note

To use the basic REPL instead (no colors, no auto-indent), set env: {PYTHON_BASIC_REPL: "1"} and remove strip-auto-indent.

Basic Usage

import sys
print(f"Hello from Python {sys.version.split()[0]}")
Hello from Python 3.12.3

List Comprehensions

squares = [x**2 for x in range(1, 11)]
print(squares)
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

Dictionaries

data = {"name": "quarto-term", "version": "0.1.0", "shells": 6}
for key, value in data.items():
    print(f"  {key}: {value}")

  name: quarto-term
  version: 0.1.0
  shells: 6

Multi-Line Expressions

def fibonacci(n):
    a, b = 0, 1
    for _ in range(n):
        a, b = b, a + b
    return a

[fibonacci(i) for i in range(10)]
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34]