Command Palette

Search for a command to run...

Hectal
PHASE 0Beginner ~25 min· topic 5 of 8

Topic 0.5

The Interactive Shell (REPL)

In one line

Typing python with no file name opens the interactive shell, where Python runs each line as soon as you press Enter and shows the result. It is the fastest way to try an idea, check how something behaves, or use Python as a calculator.

Think of it like this

Tasting while you cook. You don't cook the whole meal and only then find out it needs salt; you taste a spoonful, adjust, and taste again. The interactive shell lets you taste Python one line at a time: type a line, see what it does, try the next.

Words you'll meet

New words in this topic, in plain English. Come back here whenever one feels fuzzy.

REPL
Read, Evaluate, Print, Loop: a program that runs each line you type and shows the result. Python's interactive shell is a REPL.
Prompt
The >>> symbol showing that Python is waiting for you to type.
Expression
A piece of code that produces a value, such as 2 + 3 or len("hi").
Evaluate
To work out the value of an expression.
Session
One run of the REPL, from starting it to exiting. Variables last only for the session.
Continuation prompt
The ... shown when Python is waiting for the rest of a multi-line statement.
repr
The "programmer's view" of a value, which the REPL shows. For text it includes the quotes: 'Asha'.

Step by step

01Open the REPL

In a terminal, type python and press Enter. Python prints its version and a hint line, then >>>.

Nothing you type here is saved to a file. It is a scratch pad.

terminal
$ python
── expected output ──
Python 3.13.2 (main, Feb 4 2025, 14:51:09) [GCC 13.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
The date and the text in square brackets depend on your system.

02Python as a calculator

Type an expression and press Enter. Python shows its value on the next line, then a fresh prompt. _ means "the last value shown".

Notice that 10 / 4 gives 2.5: the / operator always gives a number with a decimal point. Phase 1 covers numbers. Multiplying text repeats it, so "hi" * 3 gives 'hihihi'.

REPL sessionwhole filetext
>>> 2 + 3
5
>>> _ * 10
50
>>> 10 / 4
2.5
>>> "hi" * 3
'hihihi'

03Values stay for the session

An assignment such as name = "Asha" shows nothing: it is a statement that does something (remembers a value) but produces no value to display. Typing the name alone is an expression, so its value is shown.

Look at the difference between the last two results. Typing name shows 'Asha' with quotes: that is the REPL showing the repr, the programmer's view, which makes clear the value is text. print(name) shows Asha without quotes: the reader's view.

REPL sessionwhole filetext
>>> name = "Asha"
>>> name
'Asha'
>>> print(name)
Asha
>>> len(name)
4

04Multi-line statements

Some statements need more than one line, such as a for loop. (A for loop repeats its indented lines once for each item in a list; Phase 2 teaches it.) After the first line the prompt changes to ..., meaning "I'm waiting for the rest". Indent the inner line, then press Enter on an empty line to finish.

In Python 3.13 and newer the REPL indents for you after a colon and lets you move the cursor up and down inside the block to fix it.

REPL sessionwhole filetext
>>> for n in [1, 2, 3]:
...     print(n * n)
...
1
4
9

05Errors don't end the session

If a line fails, the REPL shows the error and gives you a new prompt. Your earlier variables are still there. This makes the REPL a safe place to find out what an error looks like.

REPL sessionwhole filetext
>>> x
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'x' is not defined
>>> x = 5
>>> x
5

06Ask for help, then leave

help(len) prints the documentation for len. For longer help pages, press q to return to the prompt. dir(name) lists the names of everything a value offers; most will mean nothing yet, and that is fine.

Type exit() to leave. Your variables are gone; the terminal's own prompt returns.

REPL sessionwhole filetext
>>> help(len)
Help on built-in function len in module builtins:

len(obj, /)
    Return the number of items in a container.

>>> exit()

07REPL or file?

Use the REPL to explore: what does this function return, does this expression do what I think, what does this error look like. Use a file for anything you want to keep or run again.

The Run button on this page works like a file, not like a REPL: it runs the whole program from the top each time, and it shows only what you print.

REPL or file?diagram
Rendering diagram…

Try it yourself

  1. 1

    Predict what a file shows

    In the first example, add a line 10 * 10 at the end, with no print. Predict whether the output changes, then run it. Then wrap it in print(...) and run again.

  2. 2

    Open a real REPL

    On your computer, type python in a terminal. Try 7 * 6, then _ + 1, then "ha" * 5. Make a mistake on purpose (type hello without quotes), read the error, and carry on.

    terminal
    $ python
    ── expected output ──
    >>> 7 * 6
    42
    >>> _ + 1
    43
    >>> "ha" * 5
    'hahahaha'
  3. 3

    Use the help

    In the REPL, type help(print) and find the words sep and end in the text. Topic 1.9 uses both. Press q if the help fills the screen, then exit().

Code & diagrams

A file does not echo values New tab

In the REPL, the line 2 + 3 would show 5. In a program it calculates 5 and throws it away. Only print shows things.

Sign in to run this example in your browser.

Expected output

5
this is shown
What the REPL shows vs what print shows New tab

repr() gives the programmer's view that the REPL displays. For text it keeps the quotes, so you can tell '5' (text) from 5 (a number).

Sign in to run this example in your browser.

Expected output

Asha
'Asha'
5 5
5 '5'
A calculator sessiontext

Type python in a terminal, then try these lines one at a time. The _ name holds the last result shown. round(number, 2) keeps two digits after the decimal point.

>>> 1250 * 12
15000
>>> _ / 4
3750.0
>>> price = 499
>>> price * 3
1497
>>> round(1497 * 1.18, 2)
1766.46

Break it on purpose

Errors are the best teachers. Make each change, read the error, guess what went wrong, then reveal the answer.

Break #1

Type a terminal command inside the REPL

With the >>> prompt showing, type python hello.py and press Enter.

terminal
$ >>> python hello.py
── what you'll see ──
File "<stdin>", line 1
python hello.py
^^^^^
SyntaxError: invalid syntax

Break #2

Expect a file to echo like the REPL

Save a file containing only the line 2 + 3 and run it.

sum.pywhole filepython
2 + 3
terminal
$ python sum.py

Myth vs fact

Myth

The REPL is only for beginners.

Fact

Experienced programmers use a REPL every day to check a detail in seconds. Data scientists' notebooks (Topic 16.2) are the same idea with a richer display.

Myth

Code typed in the REPL is saved somewhere.

Fact

The values vanish when you exit. Only your input history may be kept for the up-arrow key. Anything worth keeping goes in a file.

Myth

>>> is part of Python code.

Fact

It is only the prompt. When you copy an example from documentation into a file, leave out the >>> and ... at the start of the lines.

Pro corner

Extra depth for experienced readers. New to this? Skip it for now and come back later.

  • ▸

    python -i script.py runs a file and then drops you into the REPL with all its variables still alive: a quick way to inspect the state a script ends in.

  • ▸

    The 3.13 REPL is written in Python (the _pyrepl module) and replaced the old readline-based one. Setting the environment variable PYTHON_BASIC_REPL=1 brings the classic REPL back, which matters when a tool drives Python through a pipe.

  • ▸

    For daily work many people use IPython, a richer REPL with tab completion, ? help, %timeit and %run. Jupyter notebooks are IPython with a browser interface, which is why AI and data work feels so interactive.

  • ▸

    The >>> sessions in documentation can be run as tests: the doctest module (Topic 11.5) executes them and checks that the shown output still matches.

Remember this

  1. 1

    Start it by typing python (or python3) in a terminal with no file name. You see a version line and then the prompt **>>>**. That prompt means: Python is waiting for you to type something.

  2. 2

    The shell is called a REPL, short for Read, Evaluate, Print, Loop. It reads the line you typed, evaluates it (works it out), prints the result, and loops back to wait for the next line.

  3. 3

    In the REPL, an expression (anything that produces a value, like 2 + 3) shows its value automatically. You don't need print. In a file, the same line would calculate 5 and then throw it away without showing anything. This is the single biggest difference between the REPL and a script (a program saved in a .py file).

  4. 4

    The REPL remembers what you did earlier in the session: a variable (a name that remembers a value, Topic 1.1) that you set on one line is still there on the next. The special name **_** holds the last value it displayed, which is handy when using Python as a calculator. Everything is forgotten when you leave.

  5. 5

    The REPL has built-in help. A function is a named group of steps that you use by writing its name and brackets, like print(...) (Phase 5 teaches how to make your own). help(len) explains the function len, which counts items. dir("hello") lists everything you can do with a piece of text. Leave with exit() or quit(), or with Ctrl+D on macOS and Linux and Ctrl+Z then Enter on Windows.

  6. 6

    Since Python 3.13 the REPL is much friendlier. It has coloured prompts and errors. You can edit a whole multi-line block at once. The up arrow brings back a whole block. A paste mode (F3) takes larger pieces of code, and exit works without the brackets. If you are on 3.12 or older you get the plainer classic version.

Explain it without notes

01

What does REPL stand for, and what happens at each step?

02

Why does typing 2 + 3 show 5 in the REPL but nothing when it is a line in a file?

03

In the REPL, why does name show 'Asha' with quotes while print(name) shows Asha without?

04

When would you use the REPL and when a file?

Practice

01

A file contains the two lines 100 / 8 and print(100 / 8). What does running it print?

02

Write a program that prints both the reader's view and the programmer's view of the text 42.

03

You see this in documentation: >>> len("hello") followed by 5. Write the line you would put in a file to see the same answer.

Trade-offs

  • ↔

    REPL vs file. The REPL gives instant feedback but nothing is saved and long code is awkward. A file is repeatable and shareable but slower for a one-line question.

  • ↔

    REPL vs notebook. A notebook keeps code, output and notes together and is excellent for data exploration; it also makes it easy to run cells out of order and end up with hidden state (Topic 16.2). The plain REPL is always there and has no such trap.

Done when you can

  • Done when you can start and exit the Python REPL on your computer.

  • Done when you can explain what >>> and ... mean.

  • Done when you can explain why a file needs print and the REPL does not.

  • Done when you can use _, help() and exit().

  • Done when you can say when to use the REPL and when a file.