Concepts / Running Python Scripts

Running Python Scripts

The interactive interpreter automatically prints expression results; scripts do not.

  • Programming

Two Execution Contexts

Python behaves differently depending on where an expression runs. In the interactive interpreter, Python automatically shows the result of an expression. In a script, Python evaluates the expression but does not show its result unless you explicitly use print().

automatic displayevaluation onlyInteractiveinterpreter5 * 840visible resultPython script5 * 840not displayed
What happens to the result of the same expression in interactive mode compared with a script?

Tracing the Expression

Calculating 5 * 8

What happens when the expression 5 * 8 is evaluated in the interactive interpreter and in a script?

Evaluate: Python evaluates 5 * 8 and obtains the result 40.

Interactive interpreter: The interactive interpreter automatically displays the expression result, so 40 becomes visible.

Script: A script evaluates the expression but does not automatically display 40.

The calculation is evaluated in both contexts, but only the interactive interpreter automatically makes the result visible.

The important distinction is between evaluation and visibility. The expression can produce a result without that result being shown on the screen. Interactive mode is designed for quick exploration, so automatic display lets you test an idea and see the result immediately. A script is designed to perform actions and produce output in a controlled way.

Why Scripts Stay Quiet

Automatic display is useful during interactive exploration because it makes experimentation fast and intuitive. Scripts have a different purpose. They should be predictable and controlled, displaying information only when the programmer intends to communicate it. If every expression in a script automatically displayed its result, intermediate calculations and debugging information could clutter the program's output.

Choosing the Output Path

runs ininteractivescriptusedisplaysExpressionevaluatedExecution contextinteractive or scriptVisible resultinteractive modeprint()explicit output requestVisible resultscript with print()No visible outputscript without print()
How does the execution context determine whether an evaluated expression becomes visible?

To predict what a learner will see, ask two questions. First, is the expression being entered into the interactive interpreter or run as part of a script? Second, if it is in a script, has print() been used? Interactive mode supplies automatic display. A script supplies visible output only when output is explicitly requested with print().

Making Results Visible

Use print() when a result needs to be visible while a script runs. The expression provides a value for Python to evaluate, and print() provides the explicit request to display that result. This makes the script's communication deliberate rather than accidental.

evaluates togiven todisplays5 * 8expression40evaluated resultprint()explicit display requestVisible output40
How does data move from an evaluated expression to visible output when print() is used?

Suppose a learner tests the calculation 5 * 8 interactively and immediately sees 40. Moving that same expression into a script changes only the visibility behavior: the calculation is still evaluated, but the result is not shown unless the script explicitly uses print().

Mistakes Beginners Make

  • Expecting a script to display every expression result automatically

    Automatic expression display belongs to the interactive interpreter. A script evaluates the expression without displaying its result.

    Fix: Use print() when the script should show the result.

  • Treating missing output as evidence that the expression was not evaluated

    An expression in a script can be evaluated while producing no visible output.

    Fix: Separate the ideas of evaluation and display. If visibility is required, explicitly use print().

  • Assuming the different behavior is a Python error

    The difference is intentional design based on the different purposes of interactive exploration and script execution.

    Fix: Remember that scripts are designed to keep output predictable and controlled.

Prediction Practice

What do you think happens?

A learner enters the expression 5 * 8 in the interactive interpreter. Will a result be visible?

  • Yes, the interpreter automatically displays the result
  • No, an expression never displays a result
Reveal answer

Answer: Yes, the result 40 is visible.

The interactive interpreter automatically displays expression results. This automatic behavior is intended to support quick exploration.

What do you think happens?

The same expression, 5 * 8, is placed in a script without print(). Will the script display 40?

  • Yes, scripts automatically display expression results
  • No, the expression is evaluated but its result is not displayed
Reveal answer

Answer: No, the script does not display 40 automatically.

In a script, an expression produces no visible output unless it is wrapped in print().

EASY

For each situation, predict whether the expression's result is visible: an expression entered in the interactive interpreter; the same expression in a script without print(); and the same expression in a script where print() is used. Explain each prediction using the words interactive, script, automatic, and explicit.

Hints
  • Interactive mode automatically displays expression results.
  • A script does not display an expression result automatically.
  • print() explicitly requests visible output in a script.

Remember the Context

  1. The interactive interpreter automatically displays the result of an expression.
  2. A script evaluates an expression but does not display its result automatically.
  3. Use print() when a script must make an expression's result visible.
  4. The difference is intentional: interactive mode supports fast exploration, while scripts keep output predictable and controlled.

Key Takeaways

  • Interactive mode automatically shows expression results.
  • Scripts can evaluate expressions without showing their results.
  • Use print() to request visible output in a script.
  • Always identify the execution context before predicting whether an expression will display a result.