The Interactive Shell (REPL)
Learn how to use Python's interactive shell to experiment with code instantly, one line at a time.
Imagine you could ask Python a question and get an answer immediately — without writing a whole program first. That's exactly what the REPL lets you do.
The word REPL stands for Read-Eval-Print Loop. It reads what you type, evaluates (runs) it, prints the result, then waits for your next input. It loops forever until you decide to leave. Think of it as a live conversation with Python.
See it in action
— step through the idea, then dive into the details below.Python Talks Back Instantly
The REPL is a live conversation with Python — type something, press Enter, and Python replies right away. No files, no waiting.
#Starting the REPL
To open the REPL, open your terminal (also called the command line or shell) and type python — with no filename after it — then press Enter.
You'll see something like this:
$ python
Python 3.12.0 (main, ...)
Type "help", "copyright", "credits" or "license" for more information.
>>>python vs python3
On some computers you need to type python3 instead of python to get Python 3. If python opens Python 2 (you'll see "Python 2.x" in the greeting), use python3 instead.
#Typing Your First Expressions
Once you see >>>, Python is listening. Type any expression and press Enter — Python will evaluate it and print the result right away. No print() needed!
>>> 2 + 2
4
>>> 10 * 3
30
>>> 100 / 4
25.0>>> "Hello" + " " + "world"
'Hello world'
>>> len("Python")
6Think of it like a calculator
A pocket calculator shows you the result the moment you press =. The REPL works the same way — type an expression, press Enter, see the answer. The difference is that Python can do much more than arithmetic.
#Variables Work Too
You can create variables in the REPL just like in a regular Python file. They stick around for as long as the REPL session is open.
>>> name = "Ada"
>>> age = 36
>>> print(f"Hello, {name}! You are {age} years old.")
Hello, Ada! You are 36 years old.Variables disappear when you exit
Everything you type in the REPL is temporary. The moment you close the REPL session, all your variables vanish. If you want to keep your work, save it to a .py file instead.
#Exploring with help() and dir()
The REPL comes with two handy built-in tools for learning on the fly.
help(something)— shows you the official documentation for that thing.dir(something)— lists all the methods and attributes available on that thing.
>>> dir("hello")
['capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', ...]>>> help(str.upper)
Help on method_descriptor:
upper(...)
S.upper() -> str
Return a copy of S converted to uppercase.Use dir() when you forget a method name
Forgot whether it's .append() or .add() for lists? Just type dir([]) in the REPL and scan the output. No need to Google it.
#REPL vs. a .py File
The REPL is great for quick experiments, but for real programs you write code in a file ending with .py and run it with python my_script.py.
Here's a simple comparison:
- REPL — instant feedback, great for trying things out, nothing is saved.
- .py file — your code is saved, you can run it again and again, perfect for real programs.
# This is saved in a file called greet.py
name = "Ada"
print(f"Hello, {name}!")#Leaving the REPL
When you're done, you can exit the REPL in two ways:
- Type
exit()and press Enter. - Press Ctrl + D (on Mac/Linux) or Ctrl + Z then Enter (on Windows).
>>> exit()Which of the following is the correct way to open the Python REPL in your terminal?
Key takeaways
- The REPL starts when you type `python` in your terminal with no filename — look for the `>>>` prompt.
- You can type any expression and Python evaluates it instantly — perfect for experimenting.
- Variables created in the REPL are temporary; they disappear when you close the session.
- `dir()` lists what you can do with any object; `help()` explains how to do it.
- Use the REPL for quick experiments, and `.py` files for programs you want to keep.
You type the following into the Python REPL. What does Python show?
>>> 10 * 3
>>> 100 / 4A beginner wants to open the Python REPL but nothing interactive appears — Python just runs and exits. What is wrong?
$ python greet.pyComplete the REPL session so it greets Ada by name using an f-string.
>>> name = "Ada" >>> print(f"Hello, !")
Put these REPL lines in the right order to define a variable, calculate with it, and then exit.
>>> x * 10
>>> x = 5
50
>>> exit()
Open the Python REPL and do the following: 1. Calculate (15 + 7) * 3 and note the result. 2. Store your first name in a variable called name. 3. Use print() to greet yourself: "Hello, [your name]!" 4. Run dir("") and find at least one string method you haven't used before. 5. Exit the REPL using exit().
Try it live — edit the code and hit Run to execute real Python: