The Python interactive shell lets you type a line of Python and see the result immediately, without creating a file. It’s the quickest way to try an idea, check how something works, or use Python as a powerful calculator.
The shell is also called the REPL. In this tutorial, you’ll learn how to start and exit it, run calculations, create variables, write multi-line code, get built-in help, and use the time-saving keyboard shortcuts. You’ll also learn when to use the shell and when to write a script instead.
Key terms used in this article:
- Terminal: A text window where you type commands, such as Command Prompt or PowerShell on Windows and Terminal on macOS and Linux.
- Shell: A program that waits for you to type something and then responds. The Python shell responds to Python code.
- Prompt: The symbol, such as
>>>, that shows the shell is waiting for you to type. - Expression: A piece of code that produces a value, such as
2 + 3orlen("hello"). - Script: Python code saved in a
.pyfile that you can run again and again.
Table of contents
- What is the Python REPL?
- Start the Python shell
- Exit the Python shell
- Use Python as a calculator
- Automatic display vs print()
- Create and use variables
- Write multi-line code
- Errors don’t end your session
- Get help inside the shell
- Keyboard shortcuts that save time
- REPL vs script: when to use each
- Common beginner mistakes
- Optional extras
- Practice exercises
- Summary
What is the Python REPL?
REPL stands for Read, Evaluate, Print, Loop. Those four words describe exactly what the shell does:
- Read: It reads the line of code you type.
- Evaluate: It runs that code.
- Print: It shows you the result.
- Loop: It goes back to step 1 and waits for your next line.
Think of it as having a conversation with Python. You say something, Python answers straight away, and you can keep going. Nothing is saved to a file, so you can experiment freely without breaking anything.
Start the Python shell
First, make sure Python is installed. If it isn’t, follow How to Install Python on Windows, macOS, and Linux. Then open a terminal and type the command for your operating system:
Press Enter. Python prints a short welcome message and then shows the >>> prompt:
Python 3.15.1 (main, ...) on win32 Type "help", "copyright", "credits" or "license" for more information. >>>
The first line shows your Python version and system, so your text will look slightly different. The >>> prompt means Python is ready for you to type. Let’s try it:
>>> print("Hello from the Python shell!")
Hello from the Python shell!
Note: In the examples in this article, lines that start with >>> or ... are what you type (without typing the prompt itself). Lines without a prompt are Python’s answers.
Other ways to open a Python shell
- Windows: Type
pyinstead ofpythonif you installed Python with the Python install manager. You can also open Python 3.15 from the Start menu. - IDLE: Python’s simple built-in editor opens with a shell window. Search for IDLE in your Start menu or Applications folder.
- Code editors: VS Code and PyCharm have a built-in terminal where you can start the shell. PyCharm also has a Python Console window. See Setting Up VS Code, PyCharm, and Jupyter for Python.
Exit the Python shell
When you’re done, use any of these to leave the shell and return to your normal terminal:
- Type
exit()orquit()and press Enter. In Python 3.13 and newer,exitorquitwithout parentheses works too. - Windows: Press Ctrl + Z, then Enter.
- macOS and Linux: Press Ctrl + D.
Everything you created in the shell, such as variables and functions, disappears when you exit. That’s normal. To keep code, save it in a .py file.
Use Python as a calculator
Type any math expression and press Enter. Python shows the answer right away:
>>> 2 + 3 5 >>> 10 / 4 2.5 >>> 10 // 4 2 >>> 10 % 4 2 >>> 2 ** 10 1024
Here’s what each operator does:
| Operator | Meaning | Example | Result |
|---|---|---|---|
+ | Addition | 2 + 3 | 5 |
- | Subtraction | 7 - 2 | 5 |
* | Multiplication | 4 * 3 | 12 |
/ | Division (always gives a decimal) | 10 / 4 | 2.5 |
// | Floor division (drops the decimal part) | 10 // 4 | 2 |
% | Remainder after division | 10 % 4 | 2 |
** | Power | 2 ** 10 | 1024 |
Python follows the usual math order: ** first, then *, /, //, and %, then + and -. Use parentheses to change the order, for example (2 + 3) * 4 gives 20.
Reuse the last result with the underscore
In the shell, the special name _ (a single underscore) always holds the last result Python displayed. It’s handy for step-by-step calculations:
>>> 2 ** 10 1024 >>> _ * 2 2048
This shortcut only works in the interactive shell, not in scripts.
Automatic display vs print()
In the shell, you don’t need print() to see a value. If you type an expression, the shell displays its value automatically. But there’s a small difference you’ll notice with text:
>>> "Hello"
'Hello'
>>> print("Hello")
Hello
- When the shell displays a value on its own, it shows it the way you’d write it in code, so text (a string) appears with quotes. This tells you the value is text.
print()shows the value the way a user would see it, so text appears without quotes.
Important: Automatic display only happens in the interactive shell. In a .py script, a line such as 2 + 3 on its own shows nothing. Use print() in scripts whenever you want to see output.
Create and use variables
A variable stores a value under a name. In the shell, a variable keeps its value until you exit, so you can build on earlier lines:
>>> name = "Jessa" >>> name 'Jessa' >>> len(name) 5 >>> price = 250 >>> quantity = 3 >>> price * quantity 750
Notice that assigning a value (name = "Jessa") shows nothing. That’s expected. Assignment stores a value but doesn’t produce one to display. Type the variable name on its own to see its value.
The shell is also a great way to explore data types. Use type() to see what kind of value you have:
>>> type(3.5) <class 'float'> >>> type(name) <class 'str'>
Write multi-line code
Some Python code takes more than one line, such as loops, if statements, and functions. When you type a line that ends with a colon (:), the shell knows more is coming and changes the prompt to ... (three dots).
A for loop
>>> for i in range(3): ... print(i) ... 0 1 2
Follow these steps:
- Type the first line, ending with
:, and press Enter. The prompt changes to.... - Indent the next line with 4 spaces, type it, and press Enter. The new shell usually indents for you automatically.
- When the block is finished, press Enter on an empty line. This tells Python you’re done, and it runs the whole block.
An if statement
>>> x = 5
>>> if x > 3:
... print("x is big")
...
x is big
A function
>>> def square(n): ... return n * n ... >>> square(7) 49
Tip: Since Python 3.13, you can press the Up arrow to bring back a whole multi-line block (not just one line) and edit it. Since Python 3.14, the shell also colors your code as you type, which makes mistakes easier to spot.
Errors don’t end your session
Making mistakes in the shell is completely safe. If something goes wrong, Python shows an error message and then gives you a fresh prompt. Your variables are still there.
>>> 1 / 0
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
1 / 0
~~^~~
ZeroDivisionError: division by zero
>>> print("Still running!")
Still running!
Read the last line of an error first. It tells you the type of error (ZeroDivisionError) and what went wrong (division by zero). The shell is a great place to make errors on purpose and learn what they mean.
Get help inside the shell
Python has built-in documentation you can read without leaving the shell. These three functions are especially useful:
| Function | What it does | Example |
|---|---|---|
help(x) | Shows the documentation for x | help(print) |
type(x) | Shows what kind of value x is | type(10) |
dir(x) | Lists everything you can do with x | dir("abc") |
For example, help(print) explains what print() does and which options it accepts. The help text opens in a viewer: use the arrow keys or Space to scroll, and press q to close it and return to the prompt.
dir() is great for discovering what’s possible. For example, this shows the last three things you can do with a string:
>>> dir("abc")[-3:]
['translate', 'upper', 'zfill']
>>> "abc".upper()
'ABC'
Typing help() with nothing inside the parentheses starts an interactive help system. Type a topic, such as keywords, and press Enter. Type q to leave it. In Python 3.13 and newer, you can also press F1 to open it.
Keyboard shortcuts that save time
| Key | What it does |
|---|---|
| Up / Down arrows | Bring back lines (or whole blocks) you typed earlier, so you don’t have to retype them |
| Tab | Completes names. Type pri and press Tab to get print. Press Tab twice to see all options. |
| Ctrl + C | Cancels the current line, or stops code that is running too long |
| Ctrl + L | Clears the screen (your variables are kept) |
| F2 | Shows your history without the prompts and output, so it’s easy to copy (Python 3.13+) |
| F3 | Paste mode: paste a large block of code at once, then press F3 again to run it (Python 3.13+) |
REPL vs script: when to use each
The shell and a .py script work well together. Use each for what it does best:
| Interactive shell (REPL) | Script (.py file) | |
|---|---|---|
| Best for | Quick tests, calculations, exploring | Programs you want to keep and rerun |
| Is code saved? | No, it’s lost when you exit | Yes, in the file |
| Results shown | Automatically after each line | Only when you use print() |
| Runs | One line or block at a time | The whole file, top to bottom |
| Good length | A few lines | Any length |
A common workflow is to try a small idea in the shell, and once it works, copy it into your script. To learn how to run scripts, see Running Python Scripts From the Command Line.
Common beginner mistakes
1. Typing terminal commands inside the Python shell
>>> python hello.py
File "<stdin>", line 1
python hello.py
^^^^^
SyntaxError: invalid syntax
Fix: python hello.py is a terminal command, not Python code. Exit the shell first with exit(), then run the command in the terminal. If you see >>>, you’re in Python. If you see something like C:\Users\Jessa> or $, you’re in the terminal.
2. Copying the >>> prompt along with the code
If you copy an example from a tutorial including the >>> symbols, Python reports a SyntaxError. Type or paste only the code after the prompt.
3. Forgetting the empty line after a block
If you see the ... prompt and nothing happens, Python is waiting for more lines. Press Enter on an empty line to finish the block and run it.
4. Missing indentation inside a block
>>> for i in range(3):
... print(i)
File "<stdin>", line 2
print(i)
^^^^^
IndentationError: expected an indented block after 'for' statement on line 1
Fix: Lines inside a loop, if, or function must be indented, usually by 4 spaces.
5. Expecting the shell to remember your work
When you exit, everything is gone. Save anything important in a .py file.
Optional extras
You can skip this section for now. These tips become useful as you write longer programs.
- Run a script and then stay in the shell:
python -i my_script.pyruns your script and then opens the shell with all of the script’s variables available, so you can inspect them. - Start without the welcome message:
python -qstarts the shell quietly. - Run one line without opening the shell:
python -c "print(2 ** 10)"runs a single piece of code and exits. - Use the older, simpler shell: If the new shell causes problems in an unusual terminal, set the environment variable
PYTHON_BASIC_REPL=1to use the classic shell. - Try other shells: IPython and Jupyter notebooks are more powerful interactive environments, popular for data analysis.
Practice exercises
Open the Python shell and try these. Check your answers below each one.
Exercise 1: A shirt costs 499 and you buy 3. Calculate the total, then use _ to apply a 10% discount.
>>> 499 * 3 1497 >>> _ * 0.9 1347.3
Exercise 2: Store your name in a variable, then display it in capital letters and find how many characters it has.
>>> name = "Jessa" >>> name.upper() 'JESSA' >>> len(name) 5
Exercise 3: Use a for loop to print the numbers 1 to 5, each multiplied by 10.
>>> for n in range(1, 6): ... print(n * 10) ... 10 20 30 40 50
Summary
- The Python interactive shell, or REPL (Read, Evaluate, Print, Loop), runs each line you type and shows the result immediately.
- Start it with
python(Windows) orpython3(macOS and Linux). Exit withexit(), Ctrl + Z then Enter (Windows), or Ctrl + D (macOS and Linux). - Use it as a calculator. The underscore
_holds the last result. - The shell displays values automatically, with quotes around text. Scripts show output only with
print(). - Lines ending in
:start a block. Indent the next lines and press Enter on an empty line to run it. - Errors don’t end your session, so experiment freely.
- Use
help(),type(), anddir()to explore, and the Up arrow and Tab keys to save typing. - Use the shell for quick experiments and
.pyscripts for code you want to keep.
Also, See:

Leave a Reply