The REPL and running scripts
Try Python interactively in the REPL, then save your work in a .py file and run it from the terminal.
What you will learn
- Evaluate expressions and ask for help in the REPL
- Run a saved .py file from the terminal
- Explain why a script prints less than the REPL does
On this page
Python gives you two ways to run code: type it a line at a time and see the answer at once, or save it in a file and run the whole thing. You will use both every day, and they behave a little differently.
The ideas behind interpreters and programs are shared by all languages. This lesson covers only how Python does it.
The REPL
Type python3 (or py on Windows) with no file name and you land in the REPL: a prompt that Reads what you type, Evaluates it, Prints the result, and Loops back for more.
python3>>> 2 + 35>>> 12 * 784>>> "ab" * 3'ababab'The >>> is the prompt. You type the part after it, and Python prints the value of each expression. The special name _ always holds the last result, so you can build on it.
>>> 6 * 742>>> _ + 143The REPL in 3.13 and later has multi-line editing and coloured output Requires version 3.13 or later. Typing exit on its own now leaves it, with no parentheses Requires version 3.13 or later. Python 3.14 added syntax highlighting as you type Requires version 3.14 or later. On older versions, leave with exit() or press Ctrl-D (Ctrl-Z then Enter on Windows).
Getting help
help() shows documentation without leaving the prompt.
>>> help(print)Press q to close the help viewer. You can also ask for a single topic, such as help(str).
Running a script
A script is a text file of Python code, usually ending in .py. Save the following as rectangle_area.py using any text editor.
print("Area:", 12 * 7)Open a terminal in the folder that holds the file and run it.
python3 rectangle_area.pyArea: 84Python reads the file from top to bottom, runs each statement, and exits. Nothing stays in memory afterwards, so you rerun the file whenever you change it.
Why a script is quieter
In the REPL, an expression on its own line is printed. In a script it is not. The only output a script produces is what you ask for with print.
2 + 3print(2 + 3)5Only the second line prints. The first line is calculated and then thrown away, where the REPL would have echoed 5. If you copy REPL experiments into a file and the output vanishes, this is why.
Other ways to start Python
The command line has two more forms that you will see in tutorials.
python3 -c "print(2 + 3)"runs a single line of code from the command.python3 -m module_nameruns a module from the standard library or an installed package as a program. For example,python3 -m timeit "2 + 3"times a snippet. Later lessons use-mto create virtual environments.
Common pitfalls
- Running from the wrong folder. If Python says
No such file or directory, your terminal is in a different folder from the file. Usecdto move, or give the full path. - Expecting a script to echo values. Add
print(...). - Saving as
.py.txt. Some editors on Windows hide extensions. Make sure the file really ends in.py.
In the REPL, work out the area of a 12 by 7 rectangle by typing 12 * 7. Then save a script named rectangle_area.py so that running the file prints Area: 84.
Input: none. Expected output: Area: 84.
Show solution
print("Area:", 12 * 7)Area: 84In the REPL you could just type 12 * 7 to see the answer. In a file you must print it, which is why print is there.
Without running it, say exactly what this script prints, then save it and check. Input: none. The expected output is two lines.
print(10 - 4)10 * 10print("ha" * 2)Show solution
6haha10 * 10 is calculated and discarded, because a script does not echo bare expressions. The other two lines print 6 and the string repeated twice.
Summary
- The REPL (
python3) evaluates one entry at a time and echoes expression values. python3 file.pyruns a script, and onlyprintproduces output.help(),exit,-cand-mare the tools worth remembering.
Next: reading input and printing in your first real program.