Variables and Assignment: Storing Values
The interactive interpreter automatically prints expression results; scripts do not.
The Same Expression, Two Results
Consider the expression 5 * 8. If you type it into Python's interactive interpreter, Python automatically shows the expression's result. If the same expression appears in a script, Python evaluates it but does not automatically show anything on the screen. The expression has been processed in both settings; what changes is whether Python displays the result for you.
Tracing Evaluation and Display
Separate two events that are easy to confuse. First, Python evaluates the expression. Second, a result becomes visible to you. In the interactive interpreter, displaying the result happens automatically after evaluation. In a script, evaluation still happens, but displaying the result requires an explicit request with print().
The key word is automatic. In interactive mode, you do not need to ask Python to show the result of an expression. This makes the interpreter useful for quickly testing ideas, seeing results immediately, and adjusting your thinking. A script follows a different design: it performs actions and produces visible output only when you explicitly request it.
Making Script Results Visible
When an expression is in a script and you want its result to appear on the screen, wrap the expression in print(). For the source scenario, the expression 5 * 8 is evaluated in both environments. In the interactive interpreter, its result is displayed automatically. In a script, print(5 * 8) explicitly asks Python to display that result.
Checking the Same Calculation
Predict whether the result of 5 * 8 is visible in each environment.
Interactive interpreter: The expression is evaluated, and the interpreter automatically displays its result.
Script without print(): The expression is evaluated, but the script does not automatically display its result.
Script with print(): The expression is evaluated, and print() explicitly requests that the result be displayed.
The expression is visible automatically only in the interactive interpreter. In a script, use print() when the result needs to appear on the screen.
Predicting What Appears
What do you think happens?
You enter the expression 5 * 8 in a script without using print(). Will its result appear on the screen?
Reveal answer
Answer: No, because scripts require print() for visible output.
A script evaluates the expression but does not automatically display the result. The interactive interpreter provides automatic display; a script needs an explicit print() request.
For each situation, predict whether the expression's result will be visible automatically: an expression typed into the interactive interpreter; an expression placed in a script; an expression placed in a script and wrapped in print(). Then explain which environment or instruction controls the visibility.
Hints
- Look first for the execution environment: interactive interpreter or script.
- In a script, check whether print() is used.
- Keep evaluation separate from displaying the result.
Mistakes About Automatic Output
Expecting a script to behave like the interactive interpreter
The interactive interpreter automatically displays expression results, while scripts do not.
Fix:
Use print() around the expression when the script should show its result.Assuming that no visible output means the expression was not evaluated
The source distinguishes evaluation from display. A script can evaluate an expression without automatically showing its result.
Fix:
Treat evaluation and visibility as separate events. Add print() when visible output is required.Adding print() when exploring interactively because it seems required
The interactive interpreter already displays expression results automatically.
Fix:
Remember that print() is necessary for making expression results visible in scripts, not for the interpreter's automatic display.
When moving from interactive exploration to a script, review every result you expect a learner or user to see. Do not rely on an expression to display itself. In a script, explicitly use print() for the results that should become visible.
Key Takeaways
- The interactive interpreter automatically displays the result of an expression.
- A script evaluates an expression but does not automatically display its result.
- Use print() in a script when an expression's result should appear on the screen.
- The difference supports two purposes: fast, immediate exploration in the interpreter and predictable, controlled output in scripts.
- A missing display does not necessarily mean that an expression was not evaluated.
Key Takeaways
- Interactive Python automatically shows expression results.
- Scripts do not automatically show expression results, even though the expressions are evaluated.
- Use print() to make an expression result visible in a script.
- Interactive mode favors immediate exploration, while scripts favor predictable and controlled output.