VMware ESXi and vSphere Cluster Management
Python Interactive Prompt
Learn how to start, use, troubleshoot, and exit Python's interactive prompt from Windows, Linux, or macOS terminals.
What the Python interactive prompt is
The Python interactive prompt is a command-line interface where the Python interpreter accepts and runs code interactively. It is also called a REPL, which means Read-Eval-Print Loop: Python reads your input, evaluates it, displays a result when appropriate, and then waits for more input.
In an interactive session, you enter one statement or expression at a time. Python evaluates a complete command when you press Enter. The result is available immediately, which makes the prompt useful for learning syntax, trying expressions, testing functions, inspecting values, and running quick experiments.
An expression is code that produces a value, such as 2 + 3. A statement is an instruction, such as an assignment, import, or function definition.
Interactive prompt versus a saved script
A script is Python source code saved in a file, usually with a .py extension. The interactive prompt is best for short experiments and immediate feedback. A script is better when code must be reused, version-controlled, shared, run repeatedly, or organized as a larger program.
The prompt remains useful even when you primarily write files. You can use it to test a small idea before adding the code to a script or to inspect values while debugging.
Starting the interpreter
First open a terminal. A terminal is a text-based command interface used to run system commands and programs. The command interpreter inside it is called a shell. On Windows, use Command Prompt or PowerShell. On Linux or macOS, use Terminal and a shell such as Bash or Zsh.
Try the command that matches your system:
pythonMany Linux and macOS installations use python3 instead:
python3On Windows, the py launcher may be available:
pyWhen Python starts, it usually displays a startup banner. The banner includes the Python version and build or platform information, and may include guidance about commands such as help, copyright, and license.
Python 3.x.x (...) [ platform information ] on ...
Type "help", "copyright", "credits" or "license" for more information.
>>>The primary prompt, shown as >>>, confirms that the interpreter is ready for a new top-level command. Do not type the prompt characters themselves; type your Python code after them.
Starting and exiting by platform
Entering and executing code
At the >>> prompt, type a complete command and press Enter. A function call can produce output immediately:
>>> print('Hello, world!')
Hello, world!In an interactive session, Python displays the resulting value of an expression even when you do not call print():
>>> 2 + 3
5This behavior is specific to the interactive display. In a saved script, writing 2 + 3 by itself normally does not display 5; use print(2 + 3) when output is needed.
Assignments create names that remain available throughout the current session:
>>> x = 5
>>> print(x)
5
>>> x * 2
10The assignment does not display a value by itself, but later commands can use x. Variables, imported modules, and function definitions remain available until you leave or reset the interpreter.
Interactive session examples
Testing a value and an expression
>>> temperature = 21
>>> temperature
21
>>> temperature + 3
24The name temperature is created first, then reused in later expressions.
Defining and calling a function
A function definition spans multiple lines. Python uses the continuation prompt, shown as ..., while it expects more input:
>>> def greet(name):
... return f'Hello, {name}!'
...
>>> greet('Ada')
'Hello, Ada!'After the indented function body, submit a blank line to finish the definition. The function is then available for calls in the same session.
Another multiline construct
>>> if temperature > 20:
... print('Warm')
...
WarmThe ... prompt means that Python has not finished reading the current statement or block. It does not mean that Python is ignoring the input.
Code persistence and limitations
Commands entered at the interactive prompt are not automatically saved as a .py file. When you exit Python, session variables, imported modules, and definitions disappear from that interpreter session.
Use a script and an editor or IDE for reusable programs, larger programs, version control, sharing, and repeatable execution. You can still use the interactive prompt alongside files for experimentation, checking documentation, inspecting values, and debugging.
Exiting the interpreter
You can leave an interactive session with the portable commands:
>>> exit()You can also use:
>>> quit()These commands close Python and return control to the operating-system shell. The keyboard end-of-file shortcuts are:
- Windows: press
Ctrl+Z, then pressEnter. - Linux and macOS: press
Ctrl+D.
Exiting Python is different from clearing the terminal screen. A screen-clearing command only removes visible text; it does not end the Python process or discard its session state.
Troubleshooting the Python command
Python is not found
If the shell says that python or python3 is not recognized, not found, or cannot be located, the command does not resolve to an available executable. Common causes are that Python is not installed, its executable directory is missing from PATH, or the system expects a different command.
PATH is an operating-system environment variable containing directories searched for executable commands. Check the version using likely commands:
python --version
python3 --version
py --versionUse the command that works on your platform. On Windows, try py when python is unavailable. On Linux or macOS, try python3.
If Python is installed but is not on PATH, start it with the full path to the executable. The exact path depends on how and where Python was installed. You can then add the directory containing that executable to the operating system's PATH so a terminal can find Python by name. Close and reopen the terminal after changing PATH, then run a version command again.
The wrong Python version starts
Multiple installations can cause a different interpreter to start than the one you intended. Check the resolved version with python --version, python3 --version, or py --version. Use an explicit executable path or an appropriate launcher and version selector when you need a particular installation. Review PATH ordering if you want to change the persistent default.
Input does not produce a result
Python may be waiting because the statement is incomplete. Look for the ... continuation prompt. It can indicate that Python expects an indented block, a closing parenthesis, bracket, or brace, or a closing quote.
- Finish the block and submit a blank line after constructs such as a function definition.
- Check that quotes, parentheses, brackets, and braces match.
- Press
Ctrl+Cto cancel unfinished input when appropriate.
A variable or function is missing
If a name worked earlier but is missing after restarting Python, the previous interactive session ended and its state was discarded. Save reusable code in a .py file, then run or import that file in future sessions.
Exam-relevant notes
- REPL means Read-Eval-Print Loop.
>>>is the primary prompt, indicating readiness for a new top-level command....is the continuation prompt, indicating that more input is required.- Interactive expressions display their values automatically, but scripts generally require
print()for visible output. - Interactive definitions are temporary unless saved separately in a script.
- Use
Ctrl+Zfollowed byEnteron Windows andCtrl+Don Linux or macOS to send end-of-file.
Related practice: Python interactive prompt examples.