1. Give an example of a one-line script that does display something on the screen when the script is executed, but displays something different on the screen when that line is entered into the interactive shell. (using Python)
2. Give an example of a one-line script that displays something on the screen when the script is executed, but that doesn't display anything on the screen when that line is entered into the interactive shell (using Python)

1. Example: One-line Python Script Showing Different Outputs in Script vs Interactive Shell
Script:
-
When run as a script:
This line does not display anything on the screen, because string literals are ignored unless explicitly printed.
-
When entered in the interactive shell:
It displays the string 'Hello, world!'
because the interactive shell automatically echoes the value of expressions.
2. Example: One-line Python Script Displaying Output in Script, But Not in Interactive Shell
Script:
-
When run as a script:
It displays: Hello, world!
on the screen.
-
When entered in the interactive shell:
It also displays Hello, world!
, because print()
always outputs to the screen.
However, if you want a line that displays something in the script but not in the interactive shell, use a variable assignment:
But the requirement is:
So, here's an example:
So, actually, the most accurate answer is to use:
-
Script: Output appears.
-
Shell: Output appears.
But if you want a line that only outputs in script, but not when entered into shell, that's not possible with just one line and built-ins, because the shell runs code the same as the script for print statements. The closest is an assignment or expression:
-
Script: prints "Hello, world!"
-
Shell: prints "Hello, world!" and outputs nothing else (since print returns None, and the shell doesn't echo assignments or None values).
So to directly answer your question:
Final Answers
1.
2.
-
Script: Shows Hello, world!
-
Shell: Shows Hello, world!
But if you want a line that displays output only in the script and not in the shell, that's not possible with print
, but a variable assignment like below displays nothing in either.
So, the main distinction is in answer 1.
📩 Need a similar solution? Email me: adel455@hotmail.com