Python Syntax and Indentation: The Rules That Make Python Read Like English
The first thing developers notice when they come to Python from another language is the absence of curly braces. The second thing is usually an IndentationError. Once you understand why Python uses whitespace to define structure — and how to work with it consistently — the syntax feels natural fast.
What Syntax Actually Means
Syntax is the set of rules a language uses to determine whether a statement is valid. In Python, the rules are minimal but strict:
- Statements end at the end of the line (no semicolons required, though they work)
- Code blocks are defined by indentation, not braces
- The language is case-sensitive (
countandCountare different variables) - Block-opening statements end with a colon (
:)
That last point trips up many beginners. Every if, else, elif, for, while, def, class, try, except, and with statement that introduces a new block must end with a colon before the indented code begins.
How Indentation Defines Blocks
In languages like C, Java, or JavaScript, indentation is purely cosmetic — the interpreter doesn’t care about it. In Python, indentation is structural. The interpreter uses it to determine which lines belong to which block.
x = 10
if x > 5: print("x is greater than five") # inside the if block print("still inside") # also insideprint("this is outside the if block") # back at the top levelPython’s official style guide (PEP 8) recommends 4 spaces per indentation level. Most editors can be configured to insert 4 spaces when you press Tab, which is the sensible setup.
Nested blocks require nested indentation
Each level of nesting adds another 4 spaces:
for i in range(3): print(f"Outer loop: {i}") for j in range(2): print(f" Inner loop: {j}") # 8 spaces in print("Back in outer loop") # 4 spaces inThe visual hierarchy here directly matches the logical hierarchy of the code.
Common Indentation Errors
IndentationError: expected an indented block
This happens when Python expects a block but finds nothing:
def greet(name):# This will fail — the function body is emptyprint("Hello")Fix it by indenting the function body:
def greet(name): print(f"Hello, {name}")IndentationError: unexpected indent
This happens when a line is indented but shouldn’t be:
x = 5 print(x) # IndentationError — not inside any blockMixing tabs and spaces
Python 3 refuses to run code that mixes tabs and spaces in the same block. This was a source of subtle bugs in Python 2 and the restriction exists to prevent it. Configure your editor to always use spaces, and you’ll never encounter this.
The Colon is Not Optional
Every statement that opens a new block requires a colon at the end. Forgetting it is one of the most common syntax errors for new Python developers:
# Wrong — SyntaxErrorif temperature > 100 print("Too hot")
# Correctif temperature > 100: print("Too hot")This applies to functions, classes, loops, conditionals, exception handlers, and context managers — everything that introduces an indented block.
Other Key Syntax Rules
Line continuation
Long expressions can be split across lines inside brackets, braces, or parentheses without needing anything special:
# Implicit continuation inside parenthesestotal = ( first_value + second_value + third_value)
# Explicit continuation with backslash (use sparingly)result = first_value + \ second_valueThe implicit form inside brackets is preferred. Backslash continuation works but is fragile — a trailing space after the backslash will silently break it.
Multiple statements on one line
Python allows semicolons to separate statements on a single line, but this is almost never good style:
x = 1; y = 2; z = 3 # works but avoid thisComments
Single-line comments use #. There is no dedicated multi-line comment syntax — triple-quoted strings are sometimes used as a workaround, but they’re technically string literals, not comments.
# This is a comment
x = 42 # inline comments work tooA Complete Working Example
Here’s a small function that demonstrates several syntax rules together:
def classify_score(score): """Return a letter grade for a numeric score.""" if score >= 90: grade = "A" elif score >= 80: grade = "B" elif score >= 70: grade = "C" elif score >= 60: grade = "D" else: grade = "F"
return grade
# Call the functionfor test_score in [95, 82, 67, 55]: result = classify_score(test_score) print(f"Score {test_score}: {result}")Notice the pattern throughout: colons open blocks, indentation defines what’s inside them, and the structure is immediately readable without needing to count braces.
Tips for Getting Indentation Right
Configure your editor first. Before writing a line of Python, set your editor to use spaces (not tabs), 4 spaces per indent level, and display whitespace characters. VS Code, PyCharm, and most others can do this automatically.
Watch your copy-pasted code. Code copied from web pages sometimes brings mixed indentation with it. Running python -m py_compile yourfile.py catches syntax errors before you try to execute the program.
Trust the error message. When Python reports an IndentationError, it tells you the line number. Look at that line and the lines immediately above it — the problem is almost always there.
Python’s syntax feels constraining to developers used to free-form indentation, but it enforces consistency that makes code easier to read across teams and time. Once it becomes habit, most Python developers report they actually prefer it.