Sub topic 3
How the Shell Evaluates and Echoes Expressions
This chapter assumes you can start the shell and know basic Python syntax: variables, function calls, and operators. You will learn the rule that decides which lines print a result and which stay silent.
Read, evaluate, print
The interactive shell is a read-eval-print loop (REPL). It reads one complete statement, runs it, and decides whether to print something. The printing step is the part that surprises people.
The rule has two conditions. The statement must be an expression, meaning code that produces a value. Its value must also not be None. When both hold, the shell prints the value's repr (its developer-oriented text form) and stores the value in the name _.
>>> 2 + 3
5
>>> _ * 10
50Here 2 + 3 is an expression, so 5 is echoed and saved in _. The second line reuses it. Later chapters cover _ and repr in detail.
Statements stay silent
Assignments, import, def, and for are statements. They do work but are not expressions, so the shell prints nothing for them.
>>> x = 4
>>> x
4The first line produces no output. The second line is an expression (a bare name), so its value is echoed.
None is never echoed
Many functions return None to mean "no result". The shell hides it, so a call can run and return a value without any visible output.
>>> result = print("hi")
hi
>>> result
>>> result is None
TrueThe text hi comes from print() writing to the screen. It is not an echo. The call's return value was None, so nothing was echoed, and result holds that None. If you suspect a call returned None, test it with is None as shown above.
Expressions inside blocks also echo
The echo rule applies to each expression statement the shell runs, not only to top-level lines. This differs from running the same code in a script.
>>> for i in range(3):
... i * i
...
0
1
4In a script file, i * i alone on a line would compute a value and discard it. In the shell, each result is echoed. Don't rely on this when moving code into a file.
Common errors
- Expecting an assignment to echo.
x = 4prints nothing. Typexon the next line to see the value. - Treating echoed text as program output. The echo is a feature of the shell. A script that contains only
2 + 3prints nothing. - Assuming silence means failure. A silent line means a statement ran, or an expression returned
None. - Using the walrus operator and getting a surprise.
(y := 7)is an expression, so it echoes7, whiley = 7does not.
Exercise
Exercise
You type each of these lines into the shell in order: a = 10, then a + 1, then print(a + 1), then None. Which lines produce output, and what does each show?
Show answer
a = 10 is an assignment statement, so it shows nothing. a + 1 is an expression with value 11, so the shell echoes 11. print(a + 1) writes 11 to the screen as the call runs, and the call's return value is None, so no extra echo follows. The bare None is an expression whose value is None, so it shows nothing.
Exercise
What does the shell show for (n := 3), and why does it differ from n = 3?
Show answer
It echoes 3. The assignment expression := is an expression that produces a value, so the shell echoes it. The plain assignment n = 3 is a statement and produces no echo.