Python has two ways to run code: one lets you try things quickly, the other runs whole programs. Knowing the difference helps you choose the best way to work.
You want to test a small piece of code quickly without saving a file.
You are learning Python and want to see immediate results from commands.
You have a complete program saved in a file and want to run it all at once.
You want to automate tasks by running a saved Python script.
You want to debug or experiment with code step-by-step.
Syntax
Python
Interactive Mode:
Run python in terminal without a file.
Type commands one by one.
Script Mode:
Save code in a file with .py extension.
Run with: python filename.py
Interactive mode shows a prompt (usually >>>) where you type commands.
Script mode runs the whole file from top to bottom.
Examples
In interactive mode, you type print and see the output immediately.
Python
>>> print('Hello')
Hello
In script mode, save code in a file and run it to see output.
Python
# Save this in hello.pyprint('Hello from script')
Run the script file in terminal to see the output.
Python
python hello.py
Hello from script
Sample Program
This script asks for your name and then greets you. You run it in script mode.
Python
# This is a script file example
name = input('Enter your name: ')
print(f'Hello, {name}!')
OutputSuccess
Important Notes
Interactive mode is great for quick tests and learning.
Script mode is better for saving and running full programs.
You can switch between modes anytime depending on your task.
Summary
Interactive mode lets you run Python commands one at a time and see results immediately.
Script mode runs a saved file with many lines of code all at once.
Use interactive mode to experiment and script mode to run complete programs.
Practice
(1/5)
1. What is the main difference between Python interactive mode and script mode?
easy
A. Interactive mode is slower than script mode.
B. Interactive mode runs files; script mode runs commands one by one.
C. Interactive mode runs commands one by one; script mode runs a whole file at once.
D. Script mode shows results immediately after each command.
Solution
Step 1: Understand interactive mode behavior
Interactive mode lets you type and run one command at a time and see the result immediately.
Step 2: Understand script mode behavior
Script mode runs a saved file with many lines of code all at once, not line-by-line interactively.
Final Answer:
Interactive mode runs commands one by one; script mode runs a whole file at once. -> Option C
Quick Check:
Interactive = one command, Script = whole file [OK]
Hint: Interactive = one command; script = whole file run [OK]
Common Mistakes:
Confusing which mode runs commands one by one
Thinking script mode shows immediate results after each line
Believing interactive mode runs whole files
2. Which of the following is the correct way to start Python in interactive mode from a terminal?
easy
A. python script.py
B. python
C. python -i script.py
D. python run script.py
Solution
Step 1: Identify command to start interactive mode
Typing python alone in the terminal starts Python in interactive mode.
Step 2: Understand other options
python script.py runs a script, python -i script.py runs script then interactive, and python run script.py is invalid syntax.
Final Answer:
python -> Option B
Quick Check:
Just type python to start interactive mode [OK]
Hint: Type 'python' alone to start interactive mode [OK]
Common Mistakes:
Using 'python script.py' to start interactive mode
Confusing flags like -i without understanding
Typing invalid commands like 'python run script.py'
3. What will be the output if you run this code in interactive mode line by line?
print('Hello')
2 + 3
print('Done')
medium
A. Hello\nDone
B. Hello\n3\nDone
C. Hello\nNone\nDone
D. Hello\n5\nDone
Solution
Step 1: Analyze print statements in interactive mode