Table of Contents
What Is Interactive Mode?
Interactive mode is a way to use Python where you type a command, press Enter, and see the result immediately.
Instead of writing a whole script in a file and then running it, you can:
- Try out small pieces of code
- Experiment with ideas
- Test how functions work
- Do quick calculations
Interactive mode is often called:
- The Python shell
- The Python REPL (Read–Eval–Print Loop)
You can use interactive mode in:
- Your terminal/command prompt
- A built-in shell in many IDEs (for example, the Python/REPL panel)
- A special environment like IPython (more advanced; not needed for beginners)
In this chapter we’ll focus on the basic, built-in interactive mode that comes with Python.
Starting Python Interactive Mode
How you start interactive mode depends on your operating system. In all cases, you run the python (or sometimes python3) command without giving it a file name.
On Windows
- Open Command Prompt (or PowerShell).
- Type:
pythonIf that doesn’t work, try:
py- Press Enter.
If Python is installed correctly and added to your PATH, you’ll see something like:
Python 3.12.1 (tags/...) ...
Type "help", "copyright", "credits" or "license" for more information.
>>>
The >>> is the Python prompt. It means Python is waiting for you to type a command.
If python opens the Microsoft Store or is not recognized, check the installation chapter and ensure Python is set up correctly.
On macOS and Linux
- Open Terminal.
- Type:
python3
(On some systems python is enough, but python3 is safer.)
- Press Enter.
You should see something like:
Python 3.12.1 (tags/...) ...
Type "help", "copyright", "credits" or "license" for more information.
>>>
Again, the >>> means Python is ready.
Typing and Running Commands
At the >>> prompt, you can type Python code directly.
For example:
>>> 2 + 3
5
>>> print("Hello, Python!")
Hello, Python!Python will:
- Read what you typed
- Evaluate it (run it)
- Print the result (if there is one)
- Show
>>>again for the next command
Some important notes:
- Press Enter to run the current line.
- If your code is complete and valid, Python will run it immediately.
- If Python needs more lines (for example, inside a loop or
if), it will show...instead of>>>.
Example of a multi-line input:
>>> if 5 > 3:
... print("Yes, 5 is greater than 3")
...
Yes, 5 is greater than 3
>>>Explanation:
>>>is the first line....means “Python expects more lines” (you are still inside theifblock).- An empty line (pressing Enter on a blank line) tells Python you are done.
- Then Python runs the block and returns to
>>>.
Using Interactive Mode as a Calculator
Interactive mode is great for quick math:
>>> 10 + 5
15
>>> 10 - 3
7
>>> 4 * 6
24
>>> 8 / 2
4.0
>>> 2 ** 3
8You can mix operations:
>>> (2 + 3) * 4
20You can also use variables (you will learn about them in detail later):
>>> x = 10
>>> y = 3
>>> x + y
13
>>> x * y
30This is a safe way to experiment and see what Python does.
Editing and Reusing Previous Commands
Interactive mode offers some simple editing features:
- Use the Up Arrow and Down Arrow keys to go through previous commands.
- Use Left and Right Arrow to move the cursor on the line.
- Use Backspace or Delete to fix typos.
This is helpful when you make a small mistake:
>>> prin("Hello")
Traceback (most recent call last):
...
NameError: name 'prin' is not defined
Press the Up Arrow, fix prin to print, and press Enter:
>>> print("Hello")
HelloGetting Help Inside Interactive Mode
Python’s interactive mode includes a built-in help() function.
At the >>> prompt, you can type:
>>> help()You’ll see a help system prompt like:
help> Here you can:
- Type a topic (for example,
print) and press Enter. - Type
modulesto see a list of installed modules (can be long). - Type
keywordsto see Python keywords. - Type
quit(or pressCtrl + Don some systems) to leave the help system and return to>>>.
You can also ask for help about a specific object directly:
>>> help(print)
To exit help and go back to >>>, press q (for “quit”) if you see a -- More -- prompt, or follow the on-screen instructions.
Exiting Interactive Mode
When you are done, you should exit interactive mode and return to your normal terminal or command prompt.
There are several ways:
- Type:
>>> exit()- Or:
>>> quit()- Or use a keyboard shortcut:
- Windows / Linux:
Ctrl + Z, then press Enter - macOS / Linux:
Ctrl + D
After exiting, the prompt will switch back to your normal shell prompt (for example, $ on macOS/Linux or C:\> on Windows).
Anything you defined (variables, etc.) inside interactive mode is lost when you exit. Next time you start interactive mode, you start fresh.
When to Use Interactive Mode vs Scripts
You will learn more about running scripts separately, but here is how interactive mode compares:
Use interactive mode when:
- You want to try a small piece of code.
- You are learning a new function and want to see how it behaves.
- You need a quick calculation or check.
- You are exploring basic Python features.
Use a script (a .py file) when:
- Your code is longer or more complex.
- You want to save your work.
- You are building a small program or project.
A common beginner workflow:
- Experiment in interactive mode.
- Once something works as you like, copy it into a
.pyfile to make a script.
Interactive Mode in IDEs
Many IDEs provide their own interactive Python console. The idea is the same:
- A prompt (often
>>>or something similar) - Immediate feedback
- Ability to test small pieces of code
Some common patterns:
- A “Python Console” or “REPL” panel you can open.
- A button like “Run selection in Python console” to send highlighted code to the interactive prompt.
The exact steps depend on the IDE, but the behavior is similar to the terminal-based interactive mode:
- Type code
- Press Enter
- See results immediately
Practical Exercises
Try these in interactive mode to get comfortable:
- Start and exit Python:
- Open a terminal/command prompt.
- Start Python interactive mode.
- Type
1 + 1and see the result. - Exit Python properly.
- Do some calculations:
- Add, subtract, multiply, divide numbers.
- Use parentheses, for example
(5 + 3) * 2. - Test simple text output:
- Run
print("Hello, world!"). - Try
print("My name is", "Alice"). - Use a variable:
>>> a = 7
>>> b = 4
>>> a + b- Use
help: - In interactive mode, run
help(print). - Read a bit, then exit help.
Practicing these steps will make you comfortable with Python interactive mode, which is a powerful tool for learning and experimenting.