After setting up your Python environment, one of the most direct ways to start interacting with Python is through its interactive interpreter, often called the REPL. REPL stands for Read-Eval-Print Loop, which perfectly describes what it does: it reads the Python code you type in, evaluates it, prints the result (if any), and then loops back to wait for your next input. It's an excellent environment for experimenting with code snippets, checking syntax, or exploring Python's features without the overhead of creating a file.
Think of the REPL as a direct conversation with the Python interpreter. You type a command, press Enter, and Python immediately responds. This immediate feedback loop is incredibly useful for learning and quick tests.
You typically start the Python REPL by opening your terminal or command prompt and typing python
(or sometimes python3
, depending on your installation and operating system) and pressing Enter.
On Windows:
cmd
) or PowerShell.python
and press Enter.On macOS:
python3
(usually preferred on macOS to distinguish from the older system Python 2) and press Enter.On Linux:
python3
(or python
if python3
is linked to python
) and press Enter.If everything is set up correctly, you should see something like this (the exact version number and details might vary):
Python 3.10.4 (main, Mar 31 2022, 08:41:55) [GCC 7.5.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
The >>>
is the Python prompt. It indicates that the interpreter is ready to accept your commands.
Let's try some simple commands directly at the >>>
prompt.
Basic Arithmetic: Type a mathematical expression and press Enter. Python evaluates it immediately.
>>> 2 + 3
5
>>> 100 - 5 * 10
50
>>> (50 + 50) * 2 / 4
50.0
Notice how Python calculates the results and displays them on the next line.
Using print()
: The print()
function displays output.
>>> print("Hello, Python!")
Hello, Python!
Assigning Variables: You can create variables just like you would in a script.
>>> message = "Learning Python is fun"
>>> print(message)
Learning Python is fun
>>> count = 10
>>> count * 5
50
Note that simply assigning a variable (message = ...
) doesn't produce output. But if you type the variable name itself, the REPL will often print its value:
>>> message
'Learning Python is fun'
>>> count
10
The single quotes around the string 'Learning Python is fun'
indicate its data type (a string).
Multi-line Statements: For structures like if
statements or loops, the prompt changes to ...
to indicate that Python expects more input to complete the block. Press Enter on an empty line (with just the ...
prompt) to finish the block.
>>> name = "Alice"
>>> if name == "Alice":
... print("Hello, Alice!")
... else:
... print("Hello, stranger!")
...
Hello, Alice!
>>>
When you're finished using the interactive interpreter, you can exit it in a couple of ways:
exit()
and press Enter.quit()
and press Enter.Ctrl+D
(on Linux/macOS) or Ctrl+Z
followed by Enter (on Windows).You'll be returned to your regular terminal or command prompt.
The REPL is particularly useful for:
dir()
and help()
. (We'll cover these later).While you'll write most of your programs in script files (.py
files), the REPL remains a valuable companion tool throughout your Python development process. It's your sandbox for trying things out. In the next section, we'll move on to writing and running your first actual Python script file.
© 2025 ApX Machine Learning