Python Part I · Module 1 · Introduction to Python
Statements, Lines, and Why Indentation Is Not Decoration
Most languages treat the spaces at the start of a line as formatting — pleasant for humans, invisible to the compiler. Python does not. In Python, leading whitespace is part of the syntax, and a single space in the wrong place will stop your file from running at all. That trips up almost everybody once, usually when a line gets accidentally nudged while editing, and the error message is unhelpful if you do not know what it is telling you. This chapter gives you the rule that applies to everything you can currently write, shows you the error so you recognise it on sight, and gives a clearly-labelled preview of why the language works this way — without dragging in the loops and functions that make full use of it, which are still nine modules away.
Foundation9 min readPythonPart IFundamentalsIndentationSyntax
Part I · Module 1 · Chapter 1.10 · Statements, Lines, and Why Indentation Is Not Decoration
1. One Statement, One Line
A statement is one complete instruction. In Python, one statement normally occupies one line, and the end of the line ends the statement.
print("Compile")
print("Simulate")
print("Report")Compile
Simulate
ReportThree statements, three lines, carried out in the order written — the model from Chapter 1.1.
There is no ; at the end. If you have written C or SystemVerilog, that absence will feel wrong for about a day. Python uses the line ending itself as the separator, so a semicolon would be redundant.
2. The Rule for Everything You Can Currently Write
Here it is:
Every statement you write today must start at the very beginning of the line, with no spaces in front of it.
That is the whole rule for now. These three lines all start at column one — right at the left edge — and that is what Python expects.
print("Compile")
print("Simulate")3. What Happens If You Break It
Add a single space in front of the second line:
print("Compile")
print("Simulate")Run it and you get:
File "hello.py", line 2
print("Simulate")
IndentationError: unexpected indentRead what it gave you. The file, the line number, the line itself, and the name of the problem: IndentationError: unexpected indent. "Unexpected indent" means this line is pushed in and I have no reason for it to be.
Notice also that Compile did not print, even though line 1 is perfectly correct and comes first. That is Chapter 1.4 again: this is a problem with the shape of the file, so it is caught at compile time and nothing runs at all.
4. Spaces Inside a Line Are Fine
The rule is about the start of a line. Spaces elsewhere are ignored:
print( "Compile" )
print("Errors:", 0)Compile
Errors: 0Both are valid. Neither is good style — the usual convention is print("Compile") and a single space after each comma — but Python does not object.
So it is specifically the leading whitespace that carries meaning. Everything after the first non-space character is just spacing.
5. Why the Language Works This Way — Preview
This section explains why, using syntax you have not been taught. You are not expected to write any of this, and nothing here is examined. Skip to §6 if you would rather wait; nothing later in Module 1 depends on it.
In most languages, a group of lines that belong together is marked with symbols — braces in C, begin and end in SystemVerilog:
if (errors == 0) begin
print_result();
close_log();
endPython has no such markers. Instead, the indentation itself does that job:
if errors == 0:
print("Clean")
print("Closing log")
print("Done")The two indented lines belong to the if. The last line, back at the left edge, does not — it runs regardless.
That is the payoff. Indentation is not decoration in Python because it is doing the job that braces do elsewhere. Python simply requires the layout that other languages merely recommend.
Until Module 9, none of your lines have a reason to be indented. Keep everything at the left edge and you cannot hit this.
6. Tabs and Spaces
One practical warning.
Indentation can be made of spaces or tab characters, and mixing them in one file causes errors that are genuinely hard to see, because both look identical on screen.
The universal Python convention is four spaces, never tabs. Every code editor can be told to insert four spaces when you press Tab, and most do so already for .py files. If you are setting up an editor, that is the setting to check.
You will not need to indent anything until Module 9. Configure it now anyway, and the problem never arises.
7. Blank Lines
Blank lines are ignored entirely. Use them freely to separate parts of a file:
print("Compile")
print("Simulate")
print("Report")Compile
Simulate
ReportThe blank lines did nothing to the output — that is a print() with nothing in it, from Chapter 1.8 §4. They exist purely to make the file easier to read, and that is a good enough reason.
8. Predict, Run, Observe
Predict the exact output of each, then run them.
(a)
print("one")
print("two")(b)
print("one")
print("two")(c) — this one will fail. Predict what you will see, not just that it breaks:
print("one")
print("two")For (c), the useful prediction is whether one appears before the error. Chapter 1.4 §4 has the answer, and this is a good moment to confirm you believe it.
9. Exercise
Exercise 1 — cause it and read it. Take a working two-line file and put one space in front of the second line. Run it. Write down: the line number reported, the name of the error, and whether any output appeared first.
Exercise 2 — fix it blind. Have a colleague (or your past self, ten minutes later) add a stray space somewhere in a five-line file without telling you where. Run it, and use only the error message to find the line. This is the actual skill.
Exercise 3 — what is ignored. Predict which of these run without error:
(a) print( "a" ) (b) print("a") (c) print("a") with trailing spaces (d) a blank line between two prints.
Exercise 4 — check your editor. Open your editor's settings and find out whether pressing Tab inserts a tab character or spaces. If it inserts a tab, change it to four spaces. Write down which it was — you have just prevented a bug you would otherwise have met in Module 9.
10. Summary
- One statement per line, and no semicolon at the end.
- Everything you can currently write starts at the left edge, with no leading spaces.
- A stray leading space is an
IndentationError, not a style issue — and because it is a shape problem, nothing in the file runs, including correct lines above it. - Only leading whitespace carries meaning. Spaces inside a line are ignored.
- Indentation does the job braces do elsewhere — grouping lines under a header. That is previewed here and taught properly in Module 9.
- Four spaces, never tabs. Set your editor now; the problem then never occurs.
- Blank lines are ignored and are worth using for readability.
One chapter left in Module 1, and it is the one that makes everything else recoverable: what to do when Python stops and prints a message at you.
Where this fits
Part of the Python curriculum.
