Trying Code in the REPL
The REPL is Python's interactive prompt, where you type one line at a time and see its value at once, which makes it the quickest place to check a calculation or try an idea before it goes in a file.
Novice · Python · Updated
After this lesson you can
- Start the Python REPL, try expressions at its >>> prompt, and leave it with exit()
- Choose between the REPL and a .py file, knowing that only the REPL shows an expression's value without print()
Key takeaways
- Typing python with no file name starts the REPL, and its >>> prompt (read "the prompt") waits for a line of Python.
- At the prompt, Python works out each expression you type and shows its value right away, with no print() needed.
- In a .py file, an expression on its own line shows nothing. A file needs print() to show a value.
- Leave the REPL with exit(). It forgets everything you typed, so code you want to keep goes in a file.
Some questions take one line of Python to answer. How much do three bags of popcorn cost? What does Python say when you misspell print? Making a file, saving it and running it for one line is a lot of steps. Python has a faster place for questions like these, and you'll use it all through this course: to check a calculation, to try a line before it goes in a file, and to see what Python does when you aren't sure.
This chapter's example is the snack bar at Starlight Cinema: popcorn is $6.50, a soda is $3.75, there's 7% sales tax, and the snack bar calls out your name when your order is ready.
One question, a whole file
Say you want to know what three popcorns cost. With what you know so far, you'd make a file, check.py, with one line in it:
print(6.50 * 3)
19.5
Then save it, run python check.py in the terminal, and read the answer. The next question means editing the file, saving it and running it again. That's the right way to build a program, but it's slow for asking a question.
Starting the REPL
A pocket calculator answers as soon as you press the keys. Python has one of those built in, the REPL (read "repple"). The name lists what it does, over and over: Read a line, Evaluate it (work it out), Print the result, and Loop back for the next line.
Start it by typing python in VS Code's terminal, with no file name after it (on a Mac, python3, as Setup on a Mac: Python, VS Code and Git explains):
python
Python prints a line with its version and a line of tips, and then waits:
>>>
The >>> is read "the prompt": Python's prompt, waiting for a line of Python. It isn't the terminal's prompt, such as PS C:\Users\Sam\python-course> on Windows or sam@Sams-MacBook python-course % on a Mac. The prompt you see tells you who's listening. The terminal's prompt takes commands such as cd and python check.py; the >>> prompt takes Python. The program behind it, the one that reads Python and runs it, is the interpreter: typing python starts it.
Ask, and Python answers
Type something at the prompt and press Enter. Here are three tries, in one REPL session:
>>> 6.50 * 3
19.5
>>> 6.50 + 3.75
10.25
>>> "Starlight Cinema"
'Starlight Cinema'
Each line you typed is an expression: a piece of code that works out to a value. 6.50 * 3 works out to 19.5, and 6.50 + 3.75 to 10.25. The REPL shows each value straight away, with no print(). Text comes back in quotes, the way you'd write it in code, so you can tell the text '19.5' from the number 19.5.
Mistakes cost nothing here. Misspell print and Python shows the same kind of traceback you get from a file, read from the last line up, and then gives you the prompt back:
>>> prnt("Popcorn")
Traceback (most recent call last):
File "<python-input-3>", line 1, in <module>
prnt("Popcorn")
^^^^
NameError: name 'prnt' is not defined. Did you mean: 'print'?
>>>
<python-input-3> stands where a file name would be: the REPL counts the lines you've typed from 0, so this is the fourth. (Python 3.12 writes <stdin> there instead.) Nothing you typed earlier is lost.
A file is for keeping
The REPL and a file differ in two ways.
A file shows only what you print. In the REPL, an expression's value appears by itself. In a file, Python works the expression out and then throws the value away, unless print() shows it. Run this two-line file, and only the second line shows anything:
6.50 * 3
print(6.50 + 3.75)
10.25
The REPL forgets. Leave it with exit() (read "exit") and you're back at the terminal's prompt, with everything you typed gone:
>>> exit()
(Ctrl+D on a Mac, or Ctrl+Z and then Enter on Windows, leaves too.)
So use the REPL to ask a question, and a file for anything you want to run again, change later or hand in. One slip catches nearly everyone once: typing a terminal command at the >>> prompt. Python reads python check.py as Python, which it isn't:
>>> python check.py
File "<python-input-4>", line 1
python check.py
^^^^^
SyntaxError: invalid syntax
When that happens, check which prompt you're at. Leave the REPL with exit(), then run the command at the terminal's prompt.
Check yourself
2 questions to try on the lesson page.
So what
The REPL turns "I wonder what this does" into an answer in seconds, so you can try an idea before you trust it to a file.