Interactive & Script Mode
Python can be run in two ways. One is like a chat (Interactive Mode) — you type one line, Python answers at once. The other is a file (Script Mode) — you write all the lines first, then run them together. You will use both. Let's see how each one works.
1First, install Python
Python is free. Go to python.org/downloads and run the installer. On Windows, one checkbox on the first screen is very important:
py in any window and have the computer understand you. If you miss it, Python is installed but your computer cannot find it — and it will look like the installation failed.On macOS and Linux, Python is usually already installed. Either way, here is how you check. Open a terminal and type this:
# Windows — open Command Prompt, then type:
py
# macOS / Linux — open Terminal, then type:
python3If Python is there, it shows its version and then three symbols:
Python 3.13.1 (main, Dec 3 2024, 17:59:52)
Type "help", "copyright", "credits" or "license" for more information.
>>>>>> — are called the Python prompt. You do not type them. They are Python saying “I am ready, give me a line”. When you see them, you are in interactive mode.2The two modes, side by side
Instead of just reading about them, watch them. Press the buttons below. The same three lines of code are run in both modes. Notice when Python gives the answer. That is the main difference.
You type one line. Python answers at once, then waits for you again. Good for a quick test. Not good for a long program.
3Interactive mode: like a chat
It is called interactive because you and Python take turns, like two people talking. You type one line. Python answers straight away. Then it waits for your next line. Type 2 + 3 and the answer 5 appears at once.
This is useful for quick tests. Say you are not sure what print('Hi', 'there') will show. You do not have to guess. Open the prompt, type it, and Python tells you at once. Even expert programmers use it this way.
How to open the prompt on your own computer
You will need this for the next few lessons, so follow these steps once and keep the window open while you practise.
- 1Press the Windows key, type cmd and press Enter. A black window called Command Prompt opens.
- 2Type
pyand press Enter. - 3Python prints its version and shows
>>>. You are in. - 4Type a line, press Enter, and Python answers at once.
If py does not work, try python.
- 1Open the Terminal app. On most Linux systems Ctrl + Alt + T opens it. On a Mac, press Cmd + Space, type Terminal and press Enter.
- 2Type
python3and press Enter. - 3Python prints its version and shows
>>>. You are in. - 4Type a line, press Enter, and Python answers at once.
Type python (without the 3) only if python3 is not found.
Here is what it looks like once you are inside:
exit() and press Enter. (Keyboard shortcut: Ctrl + Z then Enter on Windows, Ctrl + D on Linux and macOS.) Remember: once you leave, everything you typed is gone.But interactive mode has three big problems:
Close the window and all your lines are gone. Tomorrow you start again from an empty prompt.
Once you press Enter on a line, you cannot go up and edit it. You have to type the whole line again.
Python replies after every single line. For a long program, that gets in the way.
4Script mode: a saved file
In script mode you write all your lines first, in one file. Nothing runs while you type. You save the file with a name that ends in .py — that is how the computer knows it is Python. Then you give the whole file to Python to run, and the output appears.
# write all the lines, save the file, THEN run it
print('Hello, world!')
print('My name is Ramesh.')
print(2 + 3)To run it, type this in the terminal:
python3 hello.py # macOS / Linux
py hello.py # WindowsThis fixes all three problems. The file is saved, so it is still there tomorrow. You can go back and edit any line before you run it. And Python does not interrupt — it reads the full file from top to bottom and runs everything. This is how real programs are written.
.py ending matters. If you save the same code as hello.txt, Python will not run it.5Which mode should I use?
| You want to… | Use | Why |
|---|---|---|
| Check what one line of code does | Interactive | One question, one answer, three seconds. |
| Write a program for your practical | Script | You need to save it, edit it and run it again. |
| Test one line before using it | Interactive | It is just a rough notebook. Nothing is lost. |
| Submit your work to a teacher | Script | In interactive mode there is no file to submit. |
>>> prompt, because it answers back and that makes learning easier. After the next lesson on print(), we will move to script mode and stay there.You type three lines at the >>> prompt, then close the window. What do you have?
Why must a Python program's file end in .py?