
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Using Python in Interactive Mode
Python is a high-level programming language that helps a user to run a program and integrate systems more efficiently, as it executes a program in comparatively fewer lines of code. In Python programming language, a 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 in Windows. However, the commands are almost the same in other operating systems as well.
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 simply learning Python in a hands-on way.
The Interactive mode is a live environment where we type Python commands and the interpreter immediately executes them. This mode is typically accessed through the terminal or command prompt.
Note: To run a Python code interactively, we use the command prompt in Windows and terminal in Linux and macOS, etc.
Steps to Start Interactive Mode
Following are the steps that we need to follow to use Python in Interactive mode -
- Open the terminal or command prompt in the respective operating systems.
- Type the command python in the command prompt or terminal and press Enter.
After following the above steps, we will see the command prompt as below, which means we are in Interactive mode. The Python prompt is made up of three greater-than symbols. Any valid expression can now be evaluated interactively -
>>>
Example
Here is an example which shows how we can use Interactive mode in Python -
>>> 10 + 20 30 >>> print("Welcome to Tutorialspoint") Welcome to Tutorialspoint >>> viewer = "karthikeyan riyaansh" >>> viewer.upper() 'KARTHIKEYAN RIYAANSH'
We can assign a value to a variable or accept input from the user and print its value.
>>> name = input("Enter the name:") Enter the name:Tutorialspoint >>> name 'Tutorialspoint'
How to Exit Interactive Mode?
Once we are done working with the Interactive mode, we can exit from this mode by using any method as mentioned below -
- Type exit() and press Enter.
- Press Ctrl + Z and press Enter for Windows.
- Press Ctrl + D and press Enter for Linux or macOS.
Running Scripts & Staying in Interactive Mode
When we want to run a Python script and remain in the interactive shell afterwards also then we have to use the -i flag along with the below command -
python -i sample.py
Note: Various applications like Jupyter Notebook can also allow us to run our Python code interactively.