Introduction to Python
Python REPL
Interactive Python interpreter
Interview: Quick testing and debugging
The Python REPL (Read-Eval-Print Loop) is an interactive interpreter that lets you execute Python statements one at a time and see results immediately. It's invaluable for quick testing, debugging, and exploration.
Starting the REPL
Type python3 (or python on Windows) in your terminal. You'll see the >>> prompt indicating the REPL is ready for input.
python3— start the default REPLpython3 -i script.py— run a script then drop into REPL with all variables loadedpython3 -q— quiet mode (no version banner)- Type
exit()or press Ctrl+D (Unix) / Ctrl+Z (Windows) to quit
REPL Features
- The underscore variable (_): Holds the result of the last expression — useful for chaining calculations.
- help(): Type
help(object)to get interactive documentation for any object. - dir(): Lists all attributes and methods of an object — great for exploring unfamiliar modules.
- Multi-line input: The REPL uses
...prompt for continuation lines (loops, functions, etc.). - History: Use arrow keys to navigate previous commands (requires readline support).
Enhanced REPLs
The standard REPL is basic. These alternatives provide a much richer experience:
- IPython: Syntax highlighting, tab completion, magic commands (
%timeit,%history). Install withpip install ipython. - bpython: Auto-suggestion, syntax highlighting, inline documentation.
- ptpython: Modern REPL with syntax highlighting, multi-line editing, and mouse support.
Pro Tip
Use python3 -i script.py to run a script and then stay in the REPL with all variables loaded. This is incredibly useful for debugging — you can inspect any variable after the script runs.
Use Cases
Quick calculations and data exploration
Testing code snippets before adding to scripts
Debugging by inspecting objects interactively
Learning Python by experimenting with built-in functions
Common Mistakes
Not using IPython for a better interactive experience
Forgetting about the _ (underscore) variable for last result
Not knowing about python3 -i for post-script debugging
Pasting multi-line code with leading whitespace causing IndentationError