Running Python Scripts
The interactive interpreter automatically prints expression results; scripts do not.
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().
Tracing the Expression
Calculating 5 * 8
What happens when the expression 5 * 8 is evaluated in the interactive interpreter and in a script?
- 1Evaluate
Python evaluates 5 * 8 and obtains the result 40.
- 2Interactive interpreter
The interactive interpreter automatically displays the expression result, so 40 becomes visible.
- 3Script
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
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.
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
Common mistake Expecting a script to display every expression result automatically
What it looks likeA learner evaluates 5 * 8 in a script and expects 40 to appear.
Why it failsAutomatic expression display belongs to the interactive interpreter. A script evaluates the expression without displaying its result.
Do this insteadUse print() when the script should show the result.
Common mistake Treating missing output as evidence that the expression was not evaluated
What it looks likeA script shows no visible result, so the learner assumes the calculation did not happen.
Why it failsAn expression in a script can be evaluated while producing no visible output.
Do this insteadSeparate the ideas of evaluation and display. If visibility is required, explicitly use print().
Common mistake Assuming the different behavior is a Python error
What it looks likeA learner thinks the interpreter is malfunctioning because an expression is silent in a script.
Why it failsThe difference is intentional design based on the different purposes of interactive exploration and script execution.
Do this insteadRemember 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?
Reveal 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?
Reveal answer
No, the script does not display 40 automatically.
In a script, an expression produces no visible output unless it is wrapped in print().
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
- 1The interactive interpreter automatically displays the result of an expression.
- 2A script evaluates an expression but does not display its result automatically.
- 3Use
print()when a script must make an expression's result visible. - 4The 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.