Interactive Sessions
An interactive Python session lets you type one small piece of code and see the result immediately.
It is a scratch space, not a saved program. Use it when you want to ask Python a tiny question.
Start one with:
On macOS or Linux:
python3
On Windows PowerShell:
py -3
You will see a prompt like:
>>>
At that prompt, type:
2 + 3
The session prints:
5
The box below behaves like a small browser version of that prompt. It runs Python in your browser and keeps values from one command to the next.
A small Python prompt
Run an expression. Then try value = 10, followed by value * 2.
Python will start when you run the first command.
Ready to run.
Scripts and Sessions Are Different
In an interactive session, an expression can show its value:
In a script, the same expression usually produces no visible output unless you print it:
2 + 3
That script computes 5, but you do not see it. This visible difference causes
confusion for beginners.
Use Sessions for Small Checks
Interactive sessions are useful for questions like:
- What does this expression return?
- How does this string method behave?
- What is the type of this value?
- What happens if I divide by zero?
They are not the best place for longer experiments. Once code becomes more than a few lines, put it in a script so you can save and rerun it.
A Quick Check
Try this:
The result tells you that 2 + 3 produced an integer.
You do not need to memorize every type name now. The habit matters more: ask Python small questions when you are unsure.
The Prompt Is Not Code
Do not paste the prompt itself into a script:
>>> 2 + 3
The >>> marker belongs to the interactive session. It is not Python code you
write in a .py file.
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 programmer supplied those characters.
SolutionLeave out the prompt
Do not include >>> in a script. It is an interface marker displayed by the
interactive session; the script contains only the code that follows it.
When to Use Each
Use an interactive session for tiny checks. Use a script when the code should be saved, rerun, shared, or inspected later.