Kahibaro
Discord Login Register

2.5 Python interactive mode

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:

Interactive mode is often called:

You can use interactive mode in:

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

  1. Open Command Prompt (or PowerShell).
  2. Type:
   python

If that doesn’t work, try:

   py
  1. 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

  1. Open Terminal.
  2. Type:
   python3

(On some systems python is enough, but python3 is safer.)

  1. 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:

  1. Read what you typed
  2. Evaluate it (run it)
  3. Print the result (if there is one)
  4. Show >>> again for the next command

Some important notes:

Example of a multi-line input:

>>> if 5 > 3:
...     print("Yes, 5 is greater than 3")
...
Yes, 5 is greater than 3
>>>

Explanation:

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
8

You can mix operations:

>>> (2 + 3) * 4
20

You can also use variables (you will learn about them in detail later):

>>> x = 10
>>> y = 3
>>> x + y
13
>>> x * y
30

This is a safe way to experiment and see what Python does.

Editing and Reusing Previous Commands

Interactive mode offers some simple editing features:

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")
Hello

Getting 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:

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:

  1. Type:
   >>> exit()
  1. Or:
   >>> quit()
  1. 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:

Use a script (a .py file) when:

A common beginner workflow:

  1. Experiment in interactive mode.
  2. Once something works as you like, copy it into a .py file to make a script.

Interactive Mode in IDEs

Many IDEs provide their own interactive Python console. The idea is the same:

Some common patterns:

The exact steps depend on the IDE, but the behavior is similar to the terminal-based interactive mode:

Practical Exercises

Try these in interactive mode to get comfortable:

  1. Start and exit Python:
    • Open a terminal/command prompt.
    • Start Python interactive mode.
    • Type 1 + 1 and see the result.
    • Exit Python properly.
  2. Do some calculations:
    • Add, subtract, multiply, divide numbers.
    • Use parentheses, for example (5 + 3) * 2.
  3. Test simple text output:
    • Run print("Hello, world!").
    • Try print("My name is", "Alice").
  4. Use a variable:
   >>> a = 7
   >>> b = 4
   >>> a + b
  1. 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.

Views: 75

Comments

Please login to add a comment.

Don't have an account? Register now!