RizTech Academy logo
RizTech Academy
Getting StartedLesson 5 of 510 min

The REPL: your fastest way to test an idea

Writing a file, saving it and running it is the right way to build a program. It is a clumsy way to answer a small question like "what does this do again?"

For that, Python has an interactive mode called the REPL — Read, Evaluate, Print, Loop. It reads what you type, works it out, prints the answer, and waits for the next thing.

Starting it

In your terminal, type python with no filename:

python

You get something like:

Python 3.12.4 (main, Jun  7 2024, 12:00:00)
Type "help", "copyright", "credits" or "license" for more information.
>>>

Those three arrows — >>> — are the prompt. Python is waiting.

Try it:

>>> 2 + 2
4
>>> "hello".upper()
'HELLO'
>>> len("programming")
11

Notice you did not need print. In a file, Python only shows what you ask it to show. In the REPL, it prints the result of every expression automatically. That is the whole point: you are having a conversation, not writing a program.

Getting out

Type exit() and press Enter, or press Ctrl + D on macOS and Linux, Ctrl + Z then Enter on Windows.

If you ever find yourself in a terminal where nothing you type makes sense to it and >>> is staring at you, you are in the REPL and forgot. This is how you leave.

What it is genuinely good for

Checking what something does. You half-remember that strings have a method for stripping whitespace but not what it is called. Rather than searching:

>>> "  hello  ".strip()
'hello'

Three seconds, and now you know.

Testing one line before committing to it. You are writing a program and you are not sure whether round(2.675, 2) gives what you expect. Find out first:

>>> round(2.675, 2)
2.67

That is not what most people expect — and finding it out here is far better than finding it out through a wrong number in a finished program. The module on numbers explains why it happens.

Exploring an object. dir() lists everything a thing can do, and help() explains it:

>>> dir("hello")
['__add__', '__class__', ..., 'split', 'startswith', 'strip', 'title', 'upper']

Ignore the names wrapped in double underscores for now. The readable ones at the end are the methods you can actually use.

>>> help("hello".split)

That prints the documentation for split, straight from your own machine, no internet required. Press q to get out of the help screen.

Learning to use dir() and help() is a real skill. It makes you independent of tutorials much sooner than you would expect.

What it is bad for

The REPL forgets everything when you close it. Anything you want to keep, or run twice, or show someone, belongs in a file.

It is also awkward for anything more than a couple of lines. Multi-line blocks work, but editing them is painful — there is no going back up to fix line two.

The rule of thumb: more than about three lines, or you want it tomorrow — use a file.

Two things that make it much nicer

Up arrow recalls what you typed before. Press it repeatedly to walk back through your history. You will use this constantly.

_ holds the last result:

>>> 12 * 12
144
>>> _ + 6
150

This only works in the REPL, not in a file.

A better REPL

The built-in one is fine. If you find yourself living in it, ipython is a nicer version — colours, better history, tab completion that actually works:

pip install ipython
ipython

Entirely optional, and there is an argument for staying with the plain one a while: it is the one that will be available on any machine you ever log in to.

Practice

Open the REPL and work out each of these in it, without searching:

  1. What is 17 // 5? And 17 % 5? Try to explain the difference between the two before reading on. (One gives whole divisions, the other the remainder.)
  2. Run "python is fun".title(). Then find, using dir(), a method that would reverse the capitalisation instead, and try it.
  3. Run len("hello") and then len("hello world"). Does the space count?
  4. Try 10 / 2. Why does it print 5.0 and not 5? Now try 10 // 2. This is covered properly in the numbers lesson, but form a guess now.
  5. Run help(len) and read the output.

Then close the REPL and reopen it. Notice everything is gone. That is the lesson about when to use a file.


That is module one. You have Python installed, an editor configured, a program that runs, and a way to try things quickly. Next module: variables and data types — where the language actually begins.

Stuck on this lesson?

Being stuck is part of it — but being stuck alone for three days is not. Our internship programme pairs this curriculum with code review and one-to-one help from working developers, and it is free.

About the internship