When learning Python or testing how a library works, it is useful to run code immediately without creating a .py file. Python provides “Interactive Mode (REPL)” for this purpose. REPL stands for Read, Eval, Print, Loop. It reads your input, evaluates it, prints the result, and loops back to wait for the next input.
This article explains how to start and use Python’s Interactive Mode.
Starting and Exiting Interactive Mode
To use Interactive Mode, open your terminal (Command Prompt or PowerShell on Windows) and type the following command:
python
In some environments, you may need to use python3.
python3
When the command runs, you will see Python version information and the >>> prompt. This symbol means Python is ready to accept your code.
Python 3.11.5 (main, Aug 24 2023, 15:09:37) [Clang 14.0.3 (clang-1403...)]
Type "help", "copyright", "credits" or "license" for more information.
>>>
How to Exit
To exit Interactive Mode, type exit() or use the following keyboard shortcuts:
- Linux / macOS: Press
Ctrl + D - Windows: Press
Ctrl + Zand thenEnter
Running Code in Interactive Mode
Type Python code after the >>> prompt and press Enter. The code will run immediately.
1. Defining Variables and Simple Calculations
You can define variables and perform calculations to see the results instantly.
>>> # Define variables
>>> app_name = "Data Analyzer"
>>> version = 1.05
>>> # Print variable content
>>> print(app_name)
Data Analyzer
>>> # Calculate
>>> price = 1500
>>> tax_rate = 0.1
>>> total_price = price * (1 + tax_rate)
>>> print(total_price)
1650.0
2. Using Functions and Methods
You can also try built-in functions and string methods on the spot.
>>> # Check string length
>>> len(app_name)
13
>>> # Convert to uppercase
>>> app_name.upper()
'DATA ANALYZER'
3. Importing Modules
You can import standard or external libraries to test their functions.
>>> # Import math module
>>> import math
>>> # Calculate square root
>>> math.sqrt(256)
16.0
Multi-line Code (Indentation)
You can also write multi-line code that requires indentation, such as for loops, if statements, and function definitions (def).
When you start a block (a line ending with :), the prompt changes to .... This indicates a “continuation line.”
>>> items = ["Apple", "Banana", "Cherry"]
>>>
>>> # Example of a for loop
>>> for fruit in items:
... print(f"Item: {fruit}")
...
Item: Apple
Item: Banana
Item: Cherry
>>> # Example of function definition
>>> def check_value(num):
... if num > 100:
... return "Large"
... else:
... return "Small"
...
>>> # Execute the function
>>> check_value(150)
'Large'
To finish an indented block and return to the normal >>> prompt, simply press Enter (input an empty line).
Summary
Python Interactive Mode (REPL) is a very useful tool for quickly testing code snippets or checking syntax without creating a file.
- Start with
python(orpython3). - Code entered at the
>>>prompt runs immediately. - The
...prompt means indentation (continuation) is needed. - Exit with
exit()orCtrl+D(orCtrl+Zon Windows).
Use this whenever you want to quickly check how Python works during learning or development.
