Python programs can be executed in two main ways:
Both methods are useful in different situations. Interactive mode is mainly used for testing and learning, while script mode is used for writing complete programs.
Interactive mode allows the user to write and execute Python commands one by one. The result of each command is displayed immediately.
It is also known as the Python Shell.
Open Command Prompt and type:
python
You will see something like:
>>>
>>> print("Hello Python")
Hello Python
>>> 5 + 3
8
Script mode allows you to write Python code in a file and execute it as a complete program.
The file is saved with a .py extension.
Open any editor (Notepad, VS Code, etc.) and write:
print("Hello Python")
Save the file as:
program.py
Open Command Prompt and type:
python program.py
Hello Python
| Feature | Interactive Mode | Script Mode |
|---|---|---|
| Execution | Line by line | Entire program |
| Output | Immediate | After execution |
| Code Saving | Not saved | Saved in file |
| Use Case | Testing, learning | Full programs |
| Complexity | Simple | Complex |
Use Interactive Mode when:
Use Script Mode when:
.py files.