Using the Interactive Python Prompt
Start an interactive Python session and evaluate one command at a time. Learn what the prompt means, which results appear automatically, how values persist during a session, and when a saved script is the better choice.
An interactive Python session lets you enter one small piece of code at a time. Python runs it immediately, shows any visible result, and waits for the next command.
Unlike a script, the commands are not collected in a source file. The session can still remember values while it remains open. This makes interactive Python useful for short calculations, quick questions, and small experiments.
Start an Interactive Session
Open a terminal and enter the Python command that worked in the installation lesson:
python3
or:
python
or:
py
Python displays a short startup message followed by:
>>>
This marker is the prompt. It means that the interpreter is ready for Python code. Type after the prompt and press Enter:
>>> 2 + 3
5
>>>
The final prompt shows that Python finished the calculation and is ready for another command.
Try the Browser Prompt
The prompt below runs Python in the browser. Press Enter or select Run to run one command. Use Shift+Enter when you deliberately need another input line.
Interactive Python prompt
Run 2 + 3. Then try value = 10, followed by value * 2.
Python will start when you run the first command.
Ready to run.
The browser prompt behaves like the local session for the examples in this lesson. It keeps values between commands until you restart it or reload the page.
Expressions Can Display Values
An expression is code that produces a value. In an interactive session, Python displays the value of an expression:
>>> 2 + 3
5
In a script, the expression is still evaluated, but its value is not displayed automatically:
2 + 3
To make the value visible in a script, call print():
print(2 + 3)
Both interactive display and print() can show 5, but they are different
actions. A text value makes the difference easier to see:
>>> "hello"
'hello'
>>> print("hello")
hello
The interactive display includes quotation marks to show that the value is
text. print() displays the text itself.
A Session Remembers Values
Enter an assignment:
>>> value = 10
>>>
The assignment stores 10 under the name value. It does not display a result.
The next command can use the stored value:
>>> value * 2
20
This state belongs to the current interpreter session. It is not automatically
saved in a .py file. When the session ends, the stored name is lost unless
you wrote the code in a script or saved it another way.
Clear and Restart Are Different
The browser prompt has two controls:
- Clear removes the visible command history but keeps values stored in the running session.
- Restart starts a fresh interpreter and removes values created by earlier commands.
You can verify the difference:
- run
value = 10; - select Clear;
- run
value * 2and observe that20still appears; - select Restart;
- run
value * 2again.
After the restart, Python reports that value is not defined. That error is
evidence that the earlier session state was removed.
An Error Does Not Usually End the Session
Try this in the browser prompt:
1 / 0
Python reports a division-by-zero error. The session then accepts another command. You can enter:
6 / 2
and receive 3.0.
This is useful during exploration: one failed command does not require starting the whole session again. The next lesson explains how to read the useful parts of an error report.
Ask Small Questions
Interactive sessions work well for questions that need one or two commands:
- What value does this expression produce?
- What happens when I repeat or combine this text?
- What kind of value did Python produce?
- Does this small calculation raise an error?
The built-in function type() reports the kind of a value:
>>> type(2 + 3)
<class 'int'>
Here, int means integer. You do not need to memorize Python's type names yet.
The important habit is to inspect a value when its behavior is unclear.
Leave a Local Session
To leave an interactive session running in your terminal, enter:
exit()
The Python prompt closes and the terminal returns to its ordinary command
prompt. Do not run exit() in the browser prompt on this page; Restart is
the appropriate control for that embedded session.
Keyboard alternatives also exist:
- macOS or Linux: Control+D;
- Windows: Control+Z, then Enter.
exit() is the clearest shared command when you are beginning.
The Prompt Is Not Python Code
A transcript may show:
>>> print("hello")
hello
Only print("hello") is Python code entered by the reader. The first >>> is
the prompt displayed by the interpreter, and hello is output.
A script should contain:
print("hello")
It should not contain the prompt marker or the output copied from a transcript.
Exercise: Prompt or code
Should >>> be included in a Python script?
Select one choice, then check.
HintSeparate interface from code
The interactive session displays >>> before you type. Ask whether Python or
the reader supplied those characters.
SolutionLeave out the prompt
Do not include >>> in a script. It is an interface marker displayed by the
interactive session. Copy only the code that follows it.
Exercise: Predict visible session output
What becomes visible after these two commands?
>>> value = 10
>>> value * 2
Select one choice, then check.
HintAsk what each command does
value = 10 stores a value. value * 2 is an expression that produces a
value.
SolutionOnly the expression displays a value
The assignment stores 10 under value and shows no result. The next
expression produces 20, which the interactive session displays.
Exercise: Remove stored session values
Which browser-prompt control removes names created by earlier commands?
Select one choice, then check.
HintDisplay and state are separate
One control changes what is visible. The other replaces the interpreter session.
SolutionRestart the interpreter
Select Restart to remove stored names and begin with a fresh interpreter. Clear removes the displayed history without deleting values.
When to Use a Session or Script
Use an interactive session for a short calculation or a question that takes only a few commands. Use a script when the code should be saved, rerun, shared, or inspected later.
You can now recognize the prompt, distinguish displayed expression values from printed output, use values retained during a session, and leave a local session. The next lesson uses both scripts and interactive commands to examine ordinary output and error reports.