How Python runs
What happens when you press Run: Python reads your file from the top, one line at a time, and the output is what your program says.
A program is a list of instructions
A program is a list of instructions, written in order, and Python does them in that order — line one, then line two, from the top down. The editor on this page holds the whole program; the Output pane shows what it says when it runs.
Nothing happens until you press Run. That is worth remembering while you read: the code can sit there half-finished, and the moment you press the button, the runtime reads it fresh from the first line.
Saying something: print
print() is the instruction that talks to you. Whatever is inside the brackets appears in the Output pane — not on the page, and not in the editor.
Text goes inside quotes: print("Hello!"). The quotes are not printed; they are how you mark which words are text. Without them, Python looks for a name called Hello, decides it has never heard of one, and stops with an error.
The output pane is the only witness
The editor holds text; the Output pane holds what the program did. That distinction sounds obvious until the first time you read a program and assume it prints something it never says. Nothing appears in the output unless a line asks for it, and a program that runs without printing anything looks exactly like a program that failed to start.
So when you are unsure whether a line ran, make it say so. A short print("here") above a suspicious line and another below it turn a guess into evidence: the position of the second marker tells you whether the first line finished, and a missing marker tells you the program stopped before it. Delete the markers once the question is answered.
Your first two lines
print("Hello!")
print("This is a second line.")Your first program
Press Run and watch the two lines appear. Then make it yours: change the greeting, and add a third line that says something else.
Notes for humans: comments
A line that starts with # is a comment. Python reads straight past it — it is there for the next person who opens the file, who is usually you, a week later, wondering why a line exists.
Blank lines work the same way. Python ignores them, and they split a long program into paragraphs. Use both freely: a program that reads like a paragraph is easier to fix than a wall of instructions.
When something goes wrong
Errors are normal, not a sign you cannot do this. When Python cannot understand a line it stops, and the message at the bottom of the output names the line number it stopped on. Read it from the bottom up: the last line says what kind of problem it was, and the one above points at the place.
A NameError almost always means a missing pair of quotes around text. An IndentationError means a line has the wrong amount of space in front of it — the next chapter meets those spaces properly. Both messages are instructions about a specific line, and that line number is where you look first.
Change one small thing, then press Run. A program you run after every change teaches you far more than one you try to write perfectly in one go.
The machine keeps a place in the file
Python does not work out every result before it starts. It takes the first line, does what it says, then moves to the next, carrying the values it has made so far. That is why a line can only use a name an earlier line has already created.
The same rule explains why printing is a useful way to investigate: each print reports the state at the moment the program reaches it. Put one before a line that misbehaves and one after, and the difference between them is what that line changed.
One line, one instruction
Each line is normally one instruction, and Python finishes it before starting the next. A blank line is ignored entirely — it is a paragraph break for the reader, not a command. Comments are ignored in the same way.
Inside a line, the parts in brackets are worked out first: print(2 + 3) adds before it prints. You can predict the output of a short program by reading one line at a time and writing down what each one does.
Three mistakes account for most first sessions: text without quotes (NameError), a missing or extra bracket (SyntaxError), and indentation copied from somewhere else (IndentationError). Python compiles the whole file before running any of it, so the reported line is often where the damage became visible, not where it began.
Try these next
- Print a line, then a blank line, then a second line, and check the gap in the Output pane.
- Comment out the middle line with
#and run again; the program should still work. - Delete a closing quote and read the
SyntaxError; put it back before moving on.
A list you can read
Print a three-line packing list with a heading and a line of dashes. Then add a fourth item, and a blank line between the heading and the list.
Check what you learned
One question at a time. A wrong answer explains itself — take another go.
Part 1 of 5
What does print() do?
