No history yet

Standard REPL Proficiency

The REPL as a Scratchpad

You've likely used Python's interactive shell to run a quick line of code. It's the first thing many developers learn. But the Read-Eval-Print Loop, or REPL, is more than just a place to print "Hello, World!". For professionals, it's a powerful diagnostic environment, a scratchpad for testing logic before committing it to a file. Let's explore some features that make it an indispensable tool.

>>> 10 + 5
15
>>> _ * 3
45
>>> name = "python"
>>> name.upper()
'PYTHON'
>>> _
'PYTHON'

The underscore variable _ is a special feature of the interactive shell. It automatically stores the result of the last evaluated expression. This is incredibly useful for chaining operations without needing to create temporary variables. Notice how after running name.upper(), the _ variable held the string 'PYTHON'. However, variable assignments like name = "python" don't return a value, so _ remains unchanged after that line.

Control Flow and Interruptions

The REPL isn't limited to single lines. You can write multi-line statements like for loops, if blocks, and function definitions. When you start an indented block, the prompt changes from >>> to ..., indicating that Python is waiting for you to complete the block. An empty line tells the interpreter the block is finished.

>>> numbers = [1, 2, 3]
>>> for num in numbers:
...     print(num * 2)
... 
2
4
6
>>>

But what happens when your code hangs, or you make a mistake? You don't have to close the session. Pressing Ctrl-C sends a KeyboardInterrupt signal. This stops the current operation—like an infinite loop—but keeps your REPL session and all its variables intact. It's a quick way to abort an action without losing your work.

When you're ready to end the session, you can use exit() or quit(). A faster way is to press Ctrl-D (on Unix-like systems) or Ctrl-Z followed by Enter (on Windows). This sends an End-of-File (EOF) character, which the REPL interprets as a signal to terminate.

ShortcutActionResult
Ctrl-CKeyboardInterruptAborts current command, keeps session active
Ctrl-DEnd-of-File (EOF)Exits the REPL session entirely

Customizing Your Environment

Typing import pandas as pd or from pathlib import Path every time you start a new session is tedious. You can automate this by using the PYTHONSTARTUP environment variable. If you set this variable to the path of a Python file, the REPL will execute that file every time it starts.

For example, you could create a file named .pythonrc.py in your home directory with common imports.

# ~/.pythonrc.py

import os
import sys
from datetime import datetime

print("Custom modules loaded!")

You would then set the environment variable in your shell's configuration file (like .bashrc or .zshrc). Now, every time you launch the Python REPL, these modules will be pre-loaded and ready to use. This simple setup turns your default REPL into a personalized, powerful workspace.

This startup script is also a great place to integrate with the sys module. For example, you can customize the prompt. The primary prompt is stored in sys.ps1 (>>>) and the secondary prompt for indented blocks is in sys.ps2 (...). Changing these can add useful context, like the current time or project name, directly into your shell.

# Add to your .pythonrc.py file
import sys
sys.ps1 = '🐍 '
sys.ps2 = '... '

With this change, your REPL will look a little more personal.

Quiz Questions 1/5

In a Python REPL session, you execute the following commands:

>>> 10 * 2
20
>>> x = 5
>>> _

What is the value of the underscore _ variable after these commands are run?

Quiz Questions 2/5

Which keyboard shortcut is used to interrupt a running command (like an infinite loop) in the Python REPL without closing the session?

Mastering these REPL features helps you write and test code faster. It's a small investment in your workflow that pays off every day.