Sometimes you don't want to create a full main.py, save it, then run it just to test a tiny snippet. You just want to try something — quickly, interactively, line by line.
That's exactly what the Python interactive shell (a.k.a. the REPL) is for. It lets you type Python commands one at a time and see the result immediately.
✨Big idea: the interactive shell is a scratchpad built into Python itself. Perfect for testing a method, checking syntax, exploring a library, or just learning by experimentation — with zero setup.
A terminal is a text-based interface for typing commands to your OS — the place where you'll launch the Python shell.
| OS | Default terminal app(s) |
|---|---|
| macOS | Terminal, iTerm2 |
| Windows | Command Prompt, PowerShell, Windows Terminal |
| Linux | GNOME Terminal, Konsole, xterm, Alacritty |
Open a terminal and type:
python(or python3 on older macOS/Linux — see the python vs python3 discussion in the install chapter).
Press Enter and you'll see something like:
Python 3.12.2 (main, Mar 21 2024, 22:48:26) [Clang 14.0.3 (clang-1403.0.22.14.1)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>That blinking >>> is the primary prompt — Python is waiting for you to type a command.
At the >>> prompt, type:
print("Hello, world!")Press Enter and the shell immediately prints:
>>> print("Hello, world!")
Hello, world!
>>>The new >>> underneath the output means Python is ready for your next command. No save, no compile, no run button — just type → result → type again.
Bonus: in the REPL you can skip print() for quick checks. Just type an expression and the shell echoes its value:
>>> 2 + 2
4
>>> 'hello'.upper()
'HELLO'
The interactive shell follows a four-step cycle — the REPL:
| Letter | Step | What Python does |
|---|---|---|
| R | Read | Reads the line you typed at the >>> prompt. |
| E | Evaluate | Interprets and executes the code. |
| P | Prints the result (or any output the code produced). | |
| L | Loop | Goes back to step 1 — shows >>> again, ready for the next command. |
flowchart LR
READ["👁️ Read<br>your input at >>>"] --> EVAL["🧠 Evaluate<br>run the code"]
EVAL --> PRINT["🖨️ Print<br>show the result"]
PRINT --> LOOP["🔁 Loop<br>show >>> again"]
LOOP --> READ
That's literally the whole job of the interactive shell — it runs this loop forever until you tell it to stop.
If you type gibberish, the REPL still follows the same cycle — the "print" step just shows an error instead of a result:
>>> something random
File "<stdin>", line 1
something random
^^^^^^
SyntaxError: invalid syntax
>>>Notice that:
- The error is friendly — it tells you the file (
<stdin>= standard input, i.e. the REPL), the line, and the type of error. - The
>>>comes back immediately. The shell didn't crash; the loop just continues.
Errors in the REPL are non-destructive. Make a typo, see the error, fix it on the next line — no harm done. This is what makes the shell such a safe place to experiment.
When you're done, exit the REPL with any of:
| How to exit | Works on |
|---|---|
Type exit() and press Enter |
All OSes |
Type quit() and press Enter |
All OSes |
Press Ctrl + D |
macOS / Linux |
Press Ctrl + Z then Enter |
Windows |
Common quiz trap: typing stop(), end(), or terminate() does NOT exit the REPL — those aren't real Python commands and just throw NameError. The only built-in exit commands are exit() and quit().
When you start a block (like a function or if), the shell switches to the secondary prompt ... — it knows you're not done yet:
>>> def greet(name):
... return f"Hello, {name}!"
...
>>> greet("Poorvith")
'Hello, Poorvith!'Indent with the same rules as in a .py file (4 spaces). An empty line ends the block.
flowchart TD
Q["What are you doing?"] --> SHORT{"Quick experiment<br>or learning?"}
SHORT -- Yes --> REPL["✅ Use the REPL<br>python → >>>"]
SHORT -- No --> LONG{"Multi-file project<br>or reusable code?"}
LONG -- Yes --> EDITOR["✅ Use an editor / IDE<br>save as main.py and run it"]
LONG -- No --> EITHER["Either works —<br>pick what feels faster"]
| Use the REPL for | Use a script for |
|---|---|
| Trying a single line of code | Anything you want to save & re-run |
| Exploring a new library or function | Programs longer than a few lines |
| Checking the result of an expression | Anything that spans multiple files |
| Debugging a small snippet | Anything you'll share or deploy |
| Learning — instant feedback loop | Production code, real applications |
- 🔝 Up Arrow — recall your previous commands. Edit and re-run them without retyping.
- 💡
help(thing)— read the built-in docs for any object:help(str.upper). - 🔍
dir(thing)— list all the attributes and methods of an object:dir("hello"). - 💭
_(underscore) — stores the value of the last expression in the REPL:>>> 2 + 2then>>> _ * 10→40. - 🎁 Tab completion — type a partial name then Tab to autocomplete (works on most setups).
- 🧬 Try installing IPython (
pip install ipython) for a souped-up REPL with syntax highlighting, magic commands, and prettier output.
- ✅ Use the REPL alongside the lessons — type each new method into the shell and watch the result.
- ✅ When experimenting, run things one expression at a time so you understand each step.
- ✅ Use
help()anddir()to explore unfamiliar objects — they're built right in. - ✅ Keep the shell open in one terminal tab while editing in another — the fastest learning loop.
- ✅ Move to a
.pyfile the moment your snippet becomes more than a few lines.
- ❌ Typing
stop(),end(), orterminate()to exit — onlyexit()/quit()work. - ❌ Trying to write a full multi-file project in the REPL — it loses state when you exit; use scripts instead.
- ❌ Forgetting that the REPL discards everything when you close it. Save anything worth keeping to a file.
- ❌ Misreading the
...secondary prompt as an error — it just means "keep typing, you're inside a block". - ❌ Mixing up
Ctrl + D(Unix exit) withCtrl + Z + Enter(Windows exit). - ❌ Pasting indented code straight from a website with stray whitespace —
IndentationErroris the usual culprit.
- What is an interactive shell? → A program that lets you type commands one at a time and see the results.
- What does REPL stand for? → Read, Evaluate, Print, Loop
- Which is a correct way to leave an interactive shell? → Type
exit()in the terminal.
- The interactive shell (REPL) lets you run Python code one line at a time — great for quick tests and learning.
- Start it by typing
python(orpython3) in a terminal. Exit withexit(),quit(),Ctrl + D(Unix), orCtrl + Z + Enter(Windows). - REPL = Read → Evaluate → Print → Loop — the cycle Python repeats forever.
- The
>>>prompt waits for input; the...prompt means "continue this block". - Errors don't crash the shell — the loop simply continues.
- Use the shell for experiments and learning, but switch to a
.pyfile for anything longer or reusable. help(),dir(), and_are your best REPL companions.
➡️
Next chapter → Python Installation Review — Local Environment & REPL Recap
Time to consolidate: a single-page recap of installing Python, running .py scripts, and using the interactive shell — everything you need to be productive in your local Python environment.