The REPL, scripts & running code
4 min lesson
- A REPL is an interactive prompt where you type one line of code and see the result immediately; a script is a saved .py file you run all at once.
- Both matter: the REPL is great for quick experiments, and scripts are how real programs are saved, shared and run.
- Think of the REPL as a conversation and a script as a letter: one is back-and-forth, the other is written once and read later.
- REPL stands for Read-Eval-Print Loop: it reads your line, evaluates it, prints the result, and loops for the next line.
- In a terminal, type python3 (or python on Windows) to open the REPL; type exit() or press Ctrl+D to leave.
- A script is run with python3 main.py; Python reads the file from top to bottom and executes each line.
- A real use: test a small calculation in the REPL, then paste the working version into a script.
- Easy-to-miss detail: the REPL forgets everything when you close it; scripts keep your code for next time.
- This connects to your first program and syntax lessons: now you can run code both ways.
- Use the REPL for quick tests and scripts for real programs.
- Open the REPL with python3 and leave with exit().
- Run a script with python3 main.py from the folder that contains it.
- Save your work in files often; the REPL does not remember after you close it.
# Calculate a small order total in a saved script.
price = 20
quantity = 3
total = price * quantity
print(f"Total: {total}")What happens: Total: 60
Breaking it down
REPL prompt
>>> 2 + 3The >>> means Python is waiting for a line. It evaluates 2 + 3 and prints 5 immediately. Without the prompt, you would not know it is ready.
Script file
main.pyA saved text file with Python code. Running it executes all lines in order. Without saving, your code disappears when you close the editor.
Run command
python3 main.pyTells Python to run the file. You must be in the folder that contains main.py, or give the full path.
REPL vs script
| Feature | REPL | Script |
|---|---|---|
| Input | One line at a time | Whole file at once |
| Memory | Forgotten when closed | Saved on disk |
| Best for | Quick experiments | Real programs |
| How to start | python3 | python3 main.py |
A helpful analogy
The REPL is like asking a friend quick questions one at a time; a script is like writing a letter with all your questions at once. Both get answers, but the letter can be saved and reread later.
Nesting rules
- 1.In the REPL, each line runs immediately; in a script, all lines run in order when you start it.
- 2.Run scripts from the folder that contains them, or use a full path.
- 3.Leave the REPL with exit() or Ctrl+D; closing the terminal also works but is less tidy.
Common mistakes
Running a script from the wrong folder
Avoid
python3 main.py // No such file or directoryUse instead
cd folder-with-main.py then python3 main.pyThe terminal looks for main.py in the current folder. If it is elsewhere, you get a file-not-found error.
Expecting REPL code to be saved
Avoid
>>> x = 5 (then close REPL)Use instead
Save x = 5 in a .py fileThe REPL keeps nothing after you exit. Save important code in a script so you can run it again.
Quick tips
- •Use the REPL to test one tricky line before adding it to your script.
- •Press the up arrow in the REPL to repeat or edit the previous line.
- •Keep scripts small at first; run them after every few lines.
- •Name your script main.py so the run command is easy to remember.