Understanding Statements vs. Expressions
The interactive interpreter automatically prints expression results; scripts do not.
The Surprising Output Difference
Suppose you enter the calculation 5 * 8 while experimenting in Python. The interactive interpreter shows a result immediately. If you place the same calculation in a script and run the script, you see no output. The calculation has still been evaluated, but the script does not automatically display its result.
What do you think happens?
What happens when 5 * 8 is entered in the interactive interpreter, and what happens when the same expression is placed in a script?
Reveal answer
The interactive interpreter displays 40, but the script displays nothing unless the result is explicitly sent to output.
The interactive interpreter automatically prints expression results. A script evaluates an expression without displaying its result unless print() is used.
Two Execution Contexts
The important word is automatically. In interactive mode, the interpreter gives you the result of an expression by default. This makes exploration quick: you can test an idea, see the result immediately, and adjust your thinking. A script follows a different design. It evaluates the expression, but it does not automatically display every intermediate result.
Expression Results in a Script
An expression is something Python evaluates to produce a result, such as the calculation 5 * 8. In the interactive interpreter, that result is automatically shown. In a script, the expression is evaluated but produces no visible output unless it is wrapped in print().
40When the expression is entered directly into the interactive interpreter, the interpreter automatically displays 40. When the same line is run as part of a script, the calculation still takes place, but no visible result appears.
Making Output Explicit
40Adding print() changes the script from merely evaluating the expression to explicitly displaying its result. Use print() when a result is intended to be part of the program's visible communication. This explicit control keeps script output predictable and prevents unwanted intermediate calculations from appearing.
Expressions and Actions
The distinction can be understood through the roles described in the source material. An expression is evaluated and produces a result. A script is intended to perform actions and produce output when explicitly told to do so. The interactive interpreter helps you inspect expression results automatically; a script requires you to decide which results should be visible.
| Context | What happens to 5 * 8 | Visible result |
|---|---|---|
| Interactive interpreter | The expression is evaluated | 40 is automatically displayed |
| Script | The expression is evaluated | Nothing is displayed automatically |
| Script with print(5 * 8) | The expression is evaluated and passed to print() | 40 is displayed explicitly |
The expression is the same; the output behavior depends on the execution context and whether print() is used.
Mistakes Beginners Make
Common mistake Expecting a script to behave like the interactive interpreter
What it looks likePutting 5 * 8 in a script and expecting 40 to appear automatically
Why it failsScripts evaluate expressions without automatically displaying their results.
Do this insteadUse print(5 * 8) when the result should be visible.
Common mistake Treating missing output as evidence that the expression was not evaluated
What it looks likeSeeing no output after running a script and assuming the calculation did not happen
Why it failsAn expression in a script can be evaluated without producing visible output.
Do this insteadDistinguish between evaluation and display. Add print() when you need to inspect or communicate the result.
Common mistake Adding automatic output to every expression
What it looks likeAssuming every intermediate calculation should be printed
Why it failsThe design of scripts is to keep output predictable and controlled rather than flooding the screen with unrequested information.
Do this insteadPrint only the results that the program is meant to communicate.
Choose the Right Output Behavior
For each situation, decide whether automatic display occurs or whether print() is needed: entering 5 * 8 directly into the interactive interpreter; placing 5 * 8 in a script; placing print(5 * 8) in a script.
Hints
- Ask whether the code is being entered interactively or run as a script.
- Then ask whether
print()explicitly requests visible output.
Predicting Visibility
Determine the visible result for 5 * 8 in three contexts.
- 1Interactive interpreter
The expression is evaluated, and the interactive interpreter automatically displays its result.
- 2Script
The expression is evaluated, but the script does not automatically display the result.
- 3Script with print()
The expression is evaluated and
print()explicitly sends the result to visible output.
The first and third contexts display 40. The plain script expression displays nothing.
Key Takeaways
- 1The interactive interpreter automatically displays the results of expressions.
- 2A script evaluates an expression but does not automatically display its result.
- 3Use
print()when an expression result should be visible in a script. - 4The difference is intentional: interactive work favors quick exploration, while scripts favor predictable and controlled output.
- 5When moving from interactive experimentation to scripts, do not rely on expressions to display themselves.
At a glance
- Interactive mode automatically shows expression results.
- Scripts evaluate expressions without automatically displaying their results.
- Use
print()to make a result visible in a script. - This behavior gives interactive exploration speed and gives scripts controlled, predictable output.