The interactive shell and the saved file, and when to use each
Two ways to run Python, with one difference that catches everyone
Type python3 on its own and press Enter. You get a banner and three angle brackets:
Python 3.12.4 (main, Jun 7 2024, 10:32:11)
Type "help", "copyright", "credits" or "license" for more information.
>>>That is the REPL — read, evaluate, print, loop. It reads one line, works out what it means, prints the answer, and waits for the next one. Try it:
>>> 2 + 2
4
>>> name = "Asha"
>>> name.upper()
'ASHA'Notice that nothing said print. The REPL prints the value of every expression automatically. That is its whole point, and it is also the trap.
Now put exactly those lines in a file called try.py and run python3 try.py. You get nothing. No output at all. The file ran correctly, computed 2 + 2, computed name.upper(), threw both answers away, and finished.
A script shows you only what you explicitly print. The REPL shows you everything. Every beginner meets this as "my code worked yesterday and prints nothing today", and it is not a bug in either place.
Which one to reach for
Use the REPL to answer a question about a single thing. What does "12".isdigit() return? Does sorted() change the original list? Is len() counting characters or bytes here? You get the answer in four seconds, and nothing is saved, which is fine because you did not want to keep it.
Use a file for anything you will run more than once, or that has more than about five lines, or that you want to show somebody. Files can be edited, corrected and re-run. The REPL cannot: a mistake on line 3 of a ten-line block means retyping the block.
Getting out, and getting back
exit()or Ctrl-D (Ctrl-Z then Enter on Windows) closes the REPL.- Everything you defined disappears when it closes. There is no saving.
- The up arrow recalls previous lines. That alone doubles the usefulness of the REPL.
The trick worth knowing
python3 -i budget.pyThe -i runs the whole file, then leaves you at a >>> prompt with every variable from the file still alive. You can poke at the data the program produced, call its functions with different arguments, and check what a value actually contains. This is a better first move than adding ten print statements, and almost nobody teaches it.
There is also python3 -c "print(2+2)", which runs one line and exits. Useful inside shell scripts and for checking whether a package is installed:
python3 -c "import pandas; print(pandas.__version__)"Indented blocks behave differently in the REPL
In a file, this is fine:
for n in [1, 2, 3]:
print(n)
print("done")Typed into the REPL, after the indented print(n) the prompt changes to ... and stays there. It is waiting to see whether the block continues. You must press Enter on an empty line to say "the block is finished". Miss that and the loop never runs, which reads as Python ignoring you.
This is the second half of the same lesson: the REPL and a file are not interchangeable. Code that works in one can need a small adjustment in the other.
One shell-only convenience
Inside the REPL, the underscore holds the value of the last expression:
>>> 1250 * 1.18
1475.0
>>> round(_, 2)
1475.0That saves retyping a long calculation to reuse its answer. It exists only in the interactive shell; in a file, _ is an ordinary name and holds nothing special. By convention it is also used in scripts for a value you are deliberately ignoring, as in for _ in range(3): when the counter is not needed.
Notebooks are a third thing
Colab and Jupyter cells behave like the REPL — the last expression in a cell prints itself — but the cells keep their values between runs and can be run in any order. That flexibility causes its own specific failure, and it gets a lesson of its own later in the course. For now: a notebook is closer to a REPL than to a file.
Try this now
Make a file check.py containing one line, 2 + 2. Run it, and confirm you get nothing. Add print(2 + 2) as a second line and run it again. Then run python3 -i check.py and type 2 + 2 at the prompt that appears.
Three runs, one idea: printing is something you ask for, except in the shell, where it is done for you.
The one thing to keep
The REPL prints the value of every expression automatically and a script prints nothing you did not ask for, so the same lines behave differently in the two places.
Before you move on
A learner tests `name.upper()` in the REPL, sees `'ASHA'`, copies the same three lines into `greet.py`, runs it, and sees no output. What has happened?
Pick the one you would defend. Nobody sees your answer.