Skip to content

Module 1 · Introduction to Python — topic 14 of 14

Interactive mode versus script mode

The two ways to run Python: typing statements at the >>> prompt for quick experiments, and running saved .py files for real programs, including how output differs between them.

Beginner 12 min read·Lesson 14 of 306 in Python Programming

Learning objectives

  • Start and leave the interactive Python shell
  • Explain how the shell echoes expression values
  • Decide when to use the shell and when to use a script

Definition

The two ways to run Python: typing statements at the >>> prompt for quick experiments, and running saved .py files for real programs, including how output differs between them.

Introduction

Python can be used in two ways. In interactive mode you type one statement at a time and see the result immediately. In script mode you save statements in a .py file and run the whole file.

Professionals use both: the shell for quick experiments and checks, and scripts for anything they want to keep.

Why it matters

The interactive shell is the fastest way to answer “what does this do?” questions while learning. Scripts are how you build, save and share programs. Knowing their differences avoids a common confusion about missing output.

Detailed explanation

Start the shell by typing python (or python3) in a terminal. The >>> prompt means Python is waiting for a statement. Lines that continue a block show the ... prompt. Leave with exit() or Ctrl+D (Ctrl+Z then Enter on Windows).

The shell is called a REPL: Read a line, Evaluate it, Print the result, Loop. If you type an expression, the shell prints its value automatically, even without print().

In a script, nothing is shown unless you call print(). A line containing only an expression, such as 2 + 3, is calculated and then silently discarded.

Variables you create in the shell vanish when you close it. A script keeps your code so you can run it again and again.

Syntax

Python shell
>>> 2 + 3
5
>>> name = "Ravi"
>>> name
'Ravi'
  • >>> marks a line you type.
  • Lines without >>> are what Python echoes back.
  • Strings are echoed with quotes because the shell shows their representation.

Code examples

In a script, bare expressions print nothing

Python
2 + 3
"hello"
print("only this line appears")
Output
only this line appears

repr() shows what the shell would echo

Python
name = "Ravi"
print(name)
print(repr(name))
Output
Ravi
'Ravi'

print shows the text; the shell (and repr) shows it with quotes.

More examples

Turning a shell experiment into a script

Python
prices = [250, 120, 75]
discount = 0.1
# In the shell you might have typed sum(prices) and seen 445.
subtotal = sum(prices)
final = subtotal * (1 - discount)
print("Subtotal:", subtotal)
print("After 10% discount:", final)
Output
Subtotal: 445
After 10% discount: 400.5

Step-by-step explanation

  • The list and discount are what you might first try out in the shell.
  • In the shell, typing sum(prices) would echo 445 automatically.
  • In the script, the values are stored in variables and printed explicitly.
  • Saving the steps in a file means the calculation can be rerun with new prices.

Expected output

Running “Turning a shell experiment into a script” prints exactly:

TextOutput
Subtotal: 445
After 10% discount: 400.5

Practical applications

  • Developers open a shell to check how a library function behaves before using it in code.
  • Data scientists use Jupyter notebooks, an enhanced interactive mode with saved cells.
  • Administrators run saved scripts on a schedule; nobody types them in each time.

Best practices

  • Use the shell to test one idea quickly, then move it into a script.
  • Keep anything you might need again in a .py file.
  • Use print() deliberately in scripts to show the results that matter.

Common mistakes

Expecting script expressions to display

Avoid

len("python") # in a .py file

Prefer

print(len("python"))

Only the shell echoes values. Scripts need print().

Pasting >>> prompts into a file

Avoid

>>> print("hi") # saved in hello.py

Prefer

print("hi")

The >>> is part of the shell display, not Python code, and causes a SyntaxError in a file.

Key takeaways

  • Interactive mode (REPL) runs one statement at a time and echoes expression values.
  • Script mode runs a saved file and shows only what you print().
  • Leave the shell with exit(); its variables are lost.

Try it yourself

  1. 1
    Open the Python shell and evaluate 15 * 4, "py" * 3 and len("interactive"). Then exit.
  2. 2
    Write the same three expressions in a script so that all three results are shown.

Finished reading? Mark it complete to update your dashboard.

Suggested practice

1 question test this exact topic. Answers and explanations are revealed after you submit.

  • BeginnerIn the interactive interpreter (the >>> prompt), what happens when you type 2 + 3 and press Enter?
Practise this topic