lesson 04 Python Setup & First Programs · beginner
Indentation
Spaces at the start of a line mean something in Python. Learn where they belong and how to fix them.
You'll be able to
- Explain why Python cares about spaces at the start of a line
- Fix an
IndentationError - Tell indentation in code from spaces inside text
The idea
Spaces at the start of a line are indentation. At the top level of a program, every line of code starts at the left edge. A line that starts with a space or a tab is an error, even when the rest of the line is fine.
Run each example to see what it prints. Running examples doesn't count toward progress.
example-1.py · One stray indent
print("Order O100043") print("Status: delivered")
Spaces inside quotes are different: they're part of the text and are printed. That's how you indent the output without indenting the code.
example-2.py · Indented output, flat code
print("Order O104839") print(" Travel Train Set")
Review
- Top-level lines start at the left edge. A space or a tab before the code is an IndentationError.
- Python reports that error before anything runs, so the console stays empty.
- Spaces inside quotes are text, and they are printed.