The REPL and running scripts

Beginner20 minPython 3.13+

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.

Terminal window
python3
Output
>>> 2 + 3
5
>>> 12 * 7
84
>>> "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 * 7
42
>>> _ + 1
43

The 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.

rectangle_area.py
print("Area:", 12 * 7)

Open a terminal in the folder that holds the file and run it.

Terminal window
python3 rectangle_area.py
Output
Area: 84

Python 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.

script_vs_repl.py
2 + 3
print(2 + 3)
Output
5

Only 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_name runs 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 -m to 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. Use cd to 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.
ExerciseFrom REPL to script

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
rectangle_area_solution.py
print("Area:", 12 * 7)
Output
Area: 84

In 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.

ExercisePredict what the script prints

Without running it, say exactly what this script prints, then save it and check. Input: none. The expected output is two lines.

predict_quiet.py
print(10 - 4)
10 * 10
print("ha" * 2)
Show solution
6
haha

10 * 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.py runs a script, and only print produces output.
  • help(), exit, -c and -m are the tools worth remembering.

Next: reading input and printing in your first real program.