Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How do we use Python in interactive mode?
Python is a high-level programming language that helps users run programs efficiently. In Python, code can be executed in two ways −
- Interactive Mode
- Script Mode
In this article, we will learn how to execute Python code in Interactive mode. The commands are similar across Windows, Linux, and macOS.
What is Interactive Mode?
Interactive mode in Python is a built-in feature that allows users to execute Python commands one line at a time. This mode is useful for testing small code snippets, debugging, or learning Python hands-on.
The Interactive mode is a live environment where you type Python commands and the interpreter immediately executes them. This mode is accessed through the terminal or command prompt.
Steps to Start Interactive Mode
Follow these steps to use Python in Interactive mode ?
- Open the terminal or command prompt
- Type
pythonorpython3and press Enter
You will see the Python prompt with three greater-than symbols, indicating you're in Interactive mode ?
>>>
Basic Interactive Operations
Arithmetic and Expressions
You can perform calculations and evaluate expressions directly ?
>>> 10 + 20 30 >>> 15 * 3 45 >>> 100 / 4 25.0
Variables and Functions
Assign values to variables and call built-in functions ?
>>> print("Welcome to TutorialsPoint")
Welcome to TutorialsPoint
>>> name = "Python Developer"
>>> name.upper()
'PYTHON DEVELOPER'
>>> len(name)
16
User Input
Accept input from users and work with the data ?
>>> user_name = input("Enter your name: ")
Enter your name: Alice
>>> user_name
'Alice'
>>> f"Hello, {user_name}!"
'Hello, Alice!'
Exiting Interactive Mode
You can exit Interactive mode using any of these methods ?
- Type
exit()orquit()and press Enter - Press Ctrl + Z then Enter (Windows)
- Press Ctrl + D (Linux/macOS)
Running Scripts in Interactive Mode
To run a Python script and remain in the interactive shell afterwards, use the -i flag ?
python -i script.py
This executes the script first, then drops you into Interactive mode with all the script's variables and functions available.
Advantages of Interactive Mode
- Immediate feedback: See results instantly
- Testing: Quickly test code snippets
- Learning: Experiment with Python syntax
- Debugging: Check variable values and function outputs
- Prototyping: Try ideas before writing full scripts
Conclusion
Interactive mode is perfect for beginners learning Python and experienced developers testing code snippets. Use python command to start, experiment with expressions and variables, then exit with exit() or keyboard shortcuts.
