Python is Indent-Sensitive
In most languages, indentation is just for humans. In Python, indentation defines code blocks. Get it wrong and your code doesn't run.
The Golden Rule
Use 4 spaces for each level. Be consistent.
if x > 0:
print("positive") # 4 spaces
if x > 100:
print("very large") # 8 spaces
print("end inner if") # 4 spaces
print("end outer if") # 0 spaces
Tabs vs Spaces
Don't mix them. Python 3 actually forbids mixing. Pick spaces and stick with it.
Most editors (including PyForm and VS Code) insert 4 spaces when you press Tab.
Common Errors
Unexpected Indent
print("hello")
print("world") # ✗ shouldn't be indented
Expected an Indented Block
if x > 0:
print("positive") # ✗ must indent
Inconsistent Indentation
if x > 0:
print("positive")
print("still here") # ✗ 2 spaces doesn't match 4
Editor Tips
- Enable "show whitespace" to see tabs vs spaces
- Set editor to convert tabs to spaces automatically
- Use auto-indent features
In PyForm
PyForm's Monaco editor auto-indents when you press Enter after a colon, and uses 4 spaces by default. You'll rarely hit IndentationError.
Write Properly-Indented Code
PyForm handles indentation automatically — focus on logic, not whitespace.
Try PyForm →