TechSchool
Home / Python3 / The Python REPL

The Python REPL

This page shows you how to start and stop the Python REPL. It also shows how to keep results, find help, and run scripts from the command line.

Start and stop the REPL

Type python3 in a terminal to start the REPL. On Windows you can also type py. The prompt >>> shows that Python waits for your input.

Type exit() or quit() to stop the REPL. You can also press Ctrl-D on Linux and macOS. On Windows, press Ctrl-Z and then Enter.

The REPL runs one statement at a time. It prints the result of each expression immediately. This makes it useful for quick tests.

Keep the last result with underscore

The name _ holds the result of the last expression. You can use it in the next line. This saves typing when you chain calculations.

The value of _ changes after each expression. It does not keep older results. Store a value in a named variable if you need it later.

Get help inside the REPL

The function help() opens the built-in help system. Call help() with no argument to read the general help text. Call help(name) to read the help for an object.

The function dir() lists the names in a scope. Call dir() with no argument to list the current names. Call dir(object) to list the attributes of an object.

Press q to leave the help viewer. The REPL prompt returns after you quit the viewer.

Import a module and call it

You can import any module at the prompt. Then call its functions in the next line. The example below imports the math module and computes a square root.

The call math.sqrt(16) returns 4.0. The result is a float, not an int. The REPL prints it at once.

An import stays active only while the REPL runs. A new session starts with no modules loaded.

import math
result = math.sqrt(16)
print(result)
print(math.pi)

Run code from the command line

The option -c runs one line of code and then exits. Use quotes around the code. The example below prints a sum and stops.

The command python3 file.py runs a script file. The REPL does not start in this mode. Python exits when the script ends.

The option -i runs a script and then opens the REPL. The REPL keeps the names that the script defined. This helps you inspect the state after the script runs.

python3 -c "print(2 + 3)"
python3 -i file.py
python3 file.py

History, Ctrl-R, and state

The REPL keeps a history of your input lines. Press the Up arrow to recall an earlier line. Press Enter to run it again.

Press Ctrl-R to search the history. Type part of an old line and the REPL finds a match. Press Ctrl-R again to move to an older match.

The REPL keeps state only in memory. A new session starts empty, so you must run your imports again. A common mistake is to call a module after a restart without the import. The fix is to run the import line first.

When a script fails, the next step is pdb. Run python3 -m pdb file.py to start the debugger. The debugger stops at the first line and lets you step through the code.

Common questions

How do I exit the Python REPL?

Type exit() or quit(), or press Ctrl-D on Linux and macOS. On Windows, press Ctrl-Z and then Enter.

What is the underscore variable?

The name _ holds the result of the last expression in the REPL. It changes after each new expression.

Why does the REPL lose my variables?

The REPL keeps state only in memory. A new session starts empty, so you must run your imports and assignments again.

Continue learning
Python3 module Python3 tutorial Python3 file io