Python online course

Using the Python Interactive Prompt

Learn how to start Python in a terminal, use the interactive prompt, understand >>> and ..., troubleshoot PATH issues, and exit the interpreter.

What Is the Python Interactive Prompt?

The Python interactive prompt is a command-line interface where the Python interpreter accepts and executes code one piece at a time. It is available inside a terminal, which is a text-based interface for entering operating-system commands.

The prompt uses a cycle called a REPL: Read input, Evaluate it, Print a result when appropriate, and Loop back for the next command. You can enter Python expressions and statements and see results immediately.

An expression produces a value, such as 2 + 3. A statement performs an instruction, such as assigning a value or importing a module. The interactive interpreter can execute both.

>>> 2 + 3
5
>>> print("Hello")
Hello

This differs from running a saved script. A script is a Python source file, normally ending in .py, that can be run again later. Commands typed at the prompt are not automatically saved as a script.

When to Use the Interactive Prompt

The prompt is useful when you want quick feedback without creating a file. Common uses include:

  • Experimenting with Python syntax
  • Learning how expressions, variables, and functions behave
  • Checking a calculation or a value
  • Testing a small code fragment
  • Investigating an installed module or built-in function

It is not usually the best place to build a persistent, multi-file application. Work typed interactively is temporary unless you manually copy it into a file or save it using another tool. For reusable programs, create a .py file and run it from the command line. An IDE or IDLE editor can make writing and saving files easier.

Starting Python from a Command Line

First open a terminal or shell:

  • On Windows, open Command Prompt or PowerShell.
  • On Linux, open a terminal window.
  • On macOS, open Terminal or another terminal application.

A shell is the command processor running inside a terminal. Command Prompt, PowerShell, and Unix shells are examples of shells. Enter one of the following commands, depending on your operating system and installation:

EnvironmentTypical commandNotes
Windows Command Prompt or PowerShellpyThe Windows Python launcher can locate an installed Python version. py -3 requests Python 3 when the launcher supports it.
Linux terminalpython3Many Linux systems use python3 to identify Python 3 explicitly.
macOS terminalpython3Use the command provided by your Python installation.
Any environment where it is configuredpythonThe command may already refer to the intended Python interpreter.

The exact command depends on the operating system, installed versions, and installation configuration. You can check a usable command's version before starting Python:

python3 --version

When Python starts, it displays a startup banner. The banner normally includes the Python version and platform information. It confirms that an interpreter started and helps you identify which version is running.

$ python3
Python 3.x.x (...)
Type "help", "copyright", "credits" or "license" for more information.
>>>

Recognizing Prompt States

A prompt is a set of characters that signals a program is ready for input. Do not confuse the operating-system shell prompt with Python's prompt.

IndicatorMeaningWhat the user should do
>>>Python is ready for a new top-level command.Enter a Python expression or statement and press Enter.
...Python expects more input because a construct is unfinished.Complete the block, closing delimiter, or multiline string. Submit a blank line when an indented block is complete.
$, %, C:\>, or PS>This is an operating-system shell prompt, not a Python prompt.Enter a shell command such as python3, py, or python.

Continuation Prompts

The ... prompt appears for a compound statement such as a loop, an expression with an open bracket, or an unfinished string. For an indented block, Python runs the block after you enter a blank line.

>>> for number in range(2):
...     print(number)
...
0
1
>>>

The indentation is part of Python syntax. A continuation prompt does not mean Python has failed; it means Python is waiting for the rest of the construct.

Entering and Executing Code

Pressing Enter submits the current line. If the line is complete, Python executes it immediately and returns to >>>.

Printed Output and Expression Results

A function call such as print() deliberately writes text to standard output, the output stream where print() normally writes:

>>> print("Hello, world!")
Hello, world!

The interpreter also displays the result of an expression entered by itself:

>>> 2 + 3
5

In a script, writing 2 + 3 alone does not normally display 5; use print(2 + 3) when output is needed. The interactive prompt automatically displays the representation of many expression results.

Variables Last for the Active Session

An assignment stores a value in a variable. The variable remains available while the same Python process is running:

>>> x = 5
>>> x
5
>>> print(x)
5

Assignment itself normally produces no displayed result. The later expression x is evaluated by the interpreter, while print(x) deliberately sends text to standard output.

Variables, imports, and functions created in one session are lost when you exit or restart that session unless you save the code elsewhere.

Basic Interactive Examples

Arithmetic

>>> 10 / 2
5.0
>>> 4 * (3 + 2)
20

Expressions are convenient for trying arithmetic operators and checking their results. See Python arithmetic operators for more examples.

Printing and Reusing a Value

>>> greeting = "Hello"
>>> print(greeting)
Hello
>>> greeting + " there"
'Hello there'

Inspecting Built-in Help

Use help() to investigate built-in functions and other Python features:

>>> help(len)

Python displays documentation for the built-in len function. In the help viewer, press q to return to the Python prompt. You can also read about Python help mode.

Errors Appear Immediately

Python reports a syntax error when it cannot parse the code. It reports an exception when a runtime problem occurs while executing otherwise parseable code.

>>> print("Missing quote)
  File "<stdin>", line 1
    print("Missing quote)
          ^
SyntaxError: unterminated string literal
>>> unknown_name
NameError: name 'unknown_name' is not defined

Read the error type, message, and indicated location, correct the command, and try again. Small isolated expressions and help() can help you investigate behavior.

Interactive Prompt Versus a Saved Script

AspectInteractive promptSaved Python script
How code is enteredTyped directly after >>> or ....Written in a file ending in .py.
When it executesUsually immediately after a complete command is submitted.When the file is run by an interpreter.
Whether work persists automaticallyNo. It is tied to the active interpreter session.Yes. The source remains in the file until changed or deleted.
Best use casesLearning, experiments, quick checks, and small investigations.Reusable programs, larger projects, and multi-file applications.

For example, save code in hello.py and run it from a Unix-like shell with:

python3 hello.py

On Windows, use the command appropriate for your installation, such as py hello.py or python hello.py. See running Python code for the saved-program workflow.

Ending a Python Session

Readable function calls are the simplest way to leave the interpreter:

>>> exit()

quit() performs the same beginner-friendly role:

>>> quit()
MethodPlatformNotes
exit()All platformsType it at the Python prompt and press Enter.
quit()All platformsA readable alternative to exit().
Ctrl+Z, then EnterWindows command shellsSends an end-of-file signal. Press Enter after Ctrl+Z.
Ctrl+DLinux and macOS terminalsSends an end-of-file signal in typical Unix-like terminals.

Keyboard behavior can vary by terminal, so exit() is a reliable alternative. After Python closes, control returns to the operating-system shell prompt.

When the Shell Cannot Find Python

If the shell reports that python or python3 is “not recognized,” “not found,” or cannot be located, the shell could not find an executable with that command name.

PATH is an environment variable containing directories that the operating system searches for executable commands. Common causes include:

  • Python is not installed.
  • The directory containing the Python executable is not in PATH.
  • Your platform uses another command name, such as py.
  • The terminal was opened before PATH was changed.

Try the appropriate alternatives:

python3 --version
py --version
python --version

On Windows, py may work even when python does not. If Python is installed but its command is not available, you can temporarily use the full executable path:

"/full/path/to/python"

Use the path syntax required by your operating system. A longer-term solution is to add the directory containing the executable to PATH, then close and reopen the terminal. See adding Python to the Windows PATH for Windows-specific guidance.

Troubleshooting Common Prompt Problems

The Wrong Python Version Starts

If python opens an unexpected or older release, multiple Python installations may exist or PATH ordering may select a different executable.

  • Check the version with python --version, python3 --version, or py --version.
  • On Windows, try py -3 to request Python 3.
  • Review PATH ordering or use an explicit interpreter path.

The Prompt Changes to ...

Complete the indented body or close the missing parenthesis, bracket, brace, or string delimiter. For a block, submit a blank line after the final indented line. If the entry is accidental or difficult to complete, press Ctrl+C to cancel it and return to >>>.

Earlier Variables Are Missing

If a variable or import is unavailable, the interpreter process may have been exited or restarted. Re-enter the setup in the current session, or save reusable code in a .py file and run or import it.

An Exit Shortcut Does Not Work

Shortcuts differ between systems and terminals. Use exit() or quit(). Remember that Windows shells generally require Ctrl+Z followed by Enter, while Linux and macOS terminals commonly use Ctrl+D.

A Command Raises an Error

A SyntaxError usually indicates invalid Python syntax. Other exceptions may indicate an undefined name or unsuitable input to a function. Read the error location and message, correct the input, and submit it again. For related error concepts, see types of Python errors.

Key Points

  • The Python prompt is a REPL for immediate, interactive execution.
  • >>> means Python is ready for a top-level command.
  • ... means Python expects more input.
  • Expression results may be displayed automatically; use print() for deliberate output.
  • Variables remain available only during the active interpreter session.
  • Use python3, python, or the Windows py launcher according to your installation.
  • Use exit() or quit() when keyboard shortcuts are uncertain.
  • Use a saved .py script for code that must persist or be reused.