Python Part I · Module 1 · Introduction to Python
The Interactive Prompt vs a .py File
There are two ways to give Python something to do, and mixing them up is the most common source of early confusion — people type a shell command into a Python file, or a Python statement into their shell, and get an error that looks unrelated to what they did. This chapter separates them properly. First you meet the interactive prompt, where you type one line and Python answers immediately, which is the fastest possible way to try something. Then you see why anything you will want again tomorrow belongs in a saved file instead. It also establishes the convention this course uses to show you which of the two you are looking at, on every page from here on.
Foundation9 min readPythonPart IFundamentalsREPLTerminal
Part I · Module 1 · Chapter 1.5 · The Interactive Prompt vs a .py File
1. Two Ways to Talk to Python
The interactive prompt. You start Python with nothing to run. It waits. You type one line, press Enter, and it responds straight away. Then it waits again. This is a conversation, and it is often called the REPL — it Reads what you typed, Evaluates it, Prints the answer, and Loops back to wait for more.
A script. You write your instructions into a file, save it, and then ask Python to run the whole file at once. Python works through it top to bottom and you see the result at the end.
Same language, same interpreter. The only difference is whether your instructions are typed live or stored in a file first.
2. Telling the Two Apart on This Site
Before going further, here is the convention this course uses. You will see three kinds of block, and confusing them is exactly the problem this chapter exists to prevent.
A terminal command — something you type at your shell, not inside a Python file:
python3Python source code — something that goes inside a .py file, or that you type at the Python prompt:
print("Simulation complete")Output — what the computer displayed back at you. You never type this:
Simulation complete3. Starting the Interactive Prompt
At your terminal, type this and press Enter:
python3Python starts and shows you something close to this. The exact version number and date will differ:
Python 3.11.4 (main, Jun 7 2023, 10:13:09) [GCC 11.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>The important part is the last line: >>>. That is Python's prompt, and it means I am waiting for you to type something.
4. Your First Interaction
At the >>> prompt, type this and press Enter:
40 * 15Python answers immediately:
600That is the whole loop. You gave it something to work out, it worked it out, it showed you the answer, and it is now waiting again.
The * means multiply — Python uses it because there is no × on a keyboard. That is the only piece of syntax in this chapter, and it is the same symbol you would use in a spreadsheet.
Try a few more. Each one answers on its own line:
600 / 4150.0Notice 150.0 rather than 150. That is not a mistake and it is not important yet — Chapter 2.6 explains exactly why division gives that answer. For now the point is only that Python answered.
5. Predict, Run, Observe
This pattern appears throughout Part I, so here is the first one properly.
Predict. Before typing anything, write down what you think Python will show for each of these:
7 * 6
100 - 1
2 * 3 * 4Run. Type each at the >>> prompt and press Enter.
Observe. Compare against your predictions.
If all three matched, you have just confirmed that the prompt does exactly what it looks like it does — which is worth knowing for certain rather than assuming.
6. Leaving
To get out of the prompt and back to your shell, type:
exit()and press Enter. The >>> disappears and your normal shell prompt returns.
On Linux and macOS, pressing Ctrl-D does the same thing.
7. Why the Prompt Is Not Enough
The prompt is excellent for one thing: checking something quickly. Does this arithmetic come out the way I think? What does this actually return? You get an answer in two seconds with nothing to set up.
It is unusable for anything else, for one decisive reason: when you close it, everything you typed is gone. There is no file. Nothing can be run again tomorrow, reviewed by a colleague, put under version control, fixed when it breaks, or launched automatically at 2 a.m.
A script solves all of that by being an ordinary file that happens to contain instructions.
| Interactive prompt | A .py file | |
|---|---|---|
| How you give instructions | one line at a time, live | write them all, then run |
| When you see results | immediately, per line | when you run the file |
| Kept after you close it | no | yes |
| Can be run again | no | yes, any number of times |
| Can be reviewed or shared | no | yes |
| Good for | trying one thing | anything you will need twice |
8. Common Mistakes
Typing a shell command at the >>> prompt. If you type python3 hello.py at the >>> prompt, Python tries to read it as Python and fails. The prompt only accepts Python.
Typing Python at the shell. The reverse also fails. 40 * 15 typed at your normal shell prompt is not a shell command.
Not noticing which prompt you are at. This is the root of both. Your shell prompt usually ends in $ or %; Python's is >>>. Glance at it before typing.
Retyping a long line at the prompt after a typo. The prompt is not an editor. Pressing the up arrow recalls your previous line for editing, which helps — but if you are editing a line repeatedly, that is the signal to put it in a file instead.
9. Exercise
Exercise 1 — the loop. Start the prompt, work out 365 * 24 in your head first, then have Python check you. Whose answer do you trust, and why is that the wrong question to ask about a tool?
Exercise 2 — cause the shell mistake deliberately. At the >>> prompt, type python3 and press Enter. Read what comes back. Now type exit() to leave, and at your normal shell prompt type 40 * 15. Read that too. Write one sentence describing what each program was complaining about.
Exercise 3 — argue for the file. You work out a calculation at the prompt that took four attempts to get right. Give two reasons why leaving it there is a problem, without using the word "lost".
10. Summary
- Two ways to give Python instructions: the interactive prompt, one line at a time with an immediate answer; or a
.pyfile, written first and run as a whole. >>>is Python's prompt. Your shell's prompt is different — usually$or%. Knowing which one you are looking at prevents the most common early error.- Three kinds of block on this site:
bashfor terminal commands,pythonfor source code, and plain output you never type. A terminal command never goes inside a.pyfile. - The prompt forgets everything when it closes. That makes it perfect for checking one thing and useless for anything you need twice.
- Predict, run, observe — decide what you expect before pressing Enter. It turns reading into learning.
You have now run Python. The next chapter makes sure you know exactly which Python you ran, which matters more than it sounds on a shared engineering machine.
Where this fits
Part of the Python curriculum.
