After this lesson you can run Python in the two ways you will use every day: interactively, one line at a time, and from a saved file. Knowing both makes learning faster, because you can test an idea in seconds and then keep the good version.
All terminal commands below are shown without output, since they depend on your system. The code examples that show a result were really run.
Way one: the REPL
REPL stands for read, evaluate, print, loop. It is an interactive session. You type a statement, Python reads it, evaluates it, prints the result and then waits for the next one.
Start it by running the Python command with no file name:
python3On Windows, use python or py. You will see a few lines about the version and then a prompt made of three greater-than signs, >>>. That means Python is waiting for you. A short session looks like this:
>>> 2 + 3
5
>>> price = 40
>>> price * 3
120
>>> exit()Notice that you did not call print to see 5 or 120. In the REPL, Python shows the value of an expression for you. Assigning a variable shows nothing, because nothing is produced. Type exit() or press the end-of-input key combination for your system to leave. On macOS and Linux that is usually Ctrl and D, and on Windows it is usually Ctrl and Z followed by Enter.
The REPL is perfect for questions such as "what does this do?" or "what does that function return?". Type it and look. Nothing is saved, so you cannot break anything.
Way two: script files
A script is a text file ending in .py. Suppose you save this as shop.py:
items = 3
price = 40
print("Bill:", items * price)Bill: 120Open a terminal in the folder that contains the file and run:
python3 shop.pyPython reads the file from the first line to the last and prints what the print calls ask for. Then it exits.
Scripts are for programs you want to keep, run again, share or improve. You edit the file, save it and run it again.
A key difference: printing
In the REPL a bare value is shown automatically. In a script it is not. This script produces no output at all:
items = 3
price = 40
items * pricePython worked out the answer and threw it away. Nobody asked it to display anything. To see it, you need print:
print(items * price)120This difference surprises many beginners, because code that seemed to work in the REPL looks silent in a file.
Where to run it from
When you type python3 shop.py, Python looks for shop.py in the current folder of the terminal. If the file is somewhere else, either move the terminal into that folder or give the full path to the file. Terminals use commands such as cd to change folder. If you get an error that says the file cannot be found, the most likely reason is that you are in a different folder.
You can check what is in the current folder with ls on macOS and Linux or dir on Windows.
Running from an editor
Code editors often have a button or shortcut that runs the current file for you. It does the same thing as the terminal command. Using the terminal at least a few times is worth it, because you will meet it everywhere in programming.
Which one should I use?
- REPL: quick tests, trying a function, checking a small calculation.
- Script: anything longer than a few lines, anything you want to repeat or change later.
A good habit is to explore an idea in the REPL, then copy the lines that worked into a file. A video version of this topic is in the video course at /learn/python/.
Common mistakes
- Typing code into the terminal instead of the REPL. If you see a normal terminal prompt,
print("hi")is not a command. Start the REPL first, or put the code in a file. - Typing
python3 shop.pyinside the REPL. That line is meant for the terminal, not for the>>>prompt. - Expecting a script to echo values. Add
print. - Running the wrong folder's file. Check the current folder if Python cannot find your script.
- Saving with the wrong extension. A file called
shop.py.txtwill not behave as a Python file. Make sure it ends in.py.
Exercise
Write a script called receipt.py that sets items to 4 and price to 25, then prints the total on a line that starts with Total:. Then say what you would see if you left out print.
Show one possible answer
items = 4
price = 25
print("Total:", items * price)Total: 100Run it with python3 receipt.py. If you left out print and wrote only items * price, the script would run without any error and show nothing, because scripts do not echo bare values the way the REPL does.
Next: give values names with variables.
Key points
- The REPL is an interactive prompt where you type one statement and see the result at once.
- A script is a .py file that you run from the terminal with python3 filename.py.
- Use the REPL for quick experiments and scripts for anything you want to keep or repeat.
- In a script, a bare expression shows nothing. Use print() when you want to see a value.