Learn Python
>>> getting_started.syntax_indentation

Python Syntax, Indentation & Comments

#concept

Most languages mark where a block of code starts and ends with braces { }. Python does it with indentation instead β€” the whitespace at the start of a line isn't just style, it's part of the syntax itself. A colon (:) at the end of a line β€” after if, for, while, def, class, and similar keywords β€” announces that an indented block follows, and everything indented at that level belongs to it.

The convention is 4 spaces per indentation level (most editors do this automatically when configured for Python), and mixing tabs with spaces in the same block causes an error rather than a silent formatting quirk. Comments, written with a #, are the other basic syntax element β€” anything after a # on a line is ignored completely by the interpreter, existing purely for human readers.

#how it works
1

A colon opens a block

Keywords like if, for, while, def, and class end their line with a colon, signaling "an indented block follows."

2

The next lines must be indented

Python expects at least one indented line right after the colon β€” 4 spaces is the standard convention.

3

Same indentation, same block

Every line indented to the same level belongs to the same block, whether that block has one line or fifty.

4

Dropping the indentation ends the block

The first line that returns to a shallower indentation level is no longer part of that block β€” no closing brace or "end" keyword needed.

5

Comments are invisible to the interpreter

Anything after a # on a line, including a whole line that starts with #, is skipped entirely when the code runs.

#example
age = 20
if age >= 18:
print("You can vote.") # inside the if-block
print("Welcome, adult!") # still inside β€” same indentation
print("This always runs.") # back to column 0 β€” outside the if-block
Output
You can vote.
Welcome, adult!
This always runs.
1Line 2 opens an if-block with a colon; Python now expects an indented line to follow.
2Lines 3 and 4 are both indented 4 spaces, so both belong to the if-block β€” they only run because age >= 18 is True.
3Line 5 returns to column 0, which tells Python the if-block has ended β€” this line runs no matter what age is.
4If age had been 15 instead, lines 3 and 4 would be skipped entirely, but line 5 would still print, since it sits outside the block.
#use cases
Reading someone else's code at a glance, since block structure is visible without hunting for matching braces
One consistent style enforced automatically β€” inconsistent indentation is a syntax error in Python, not just a style-guide complaint
Clean nesting of conditionals, loops, and functions inside each other without extra punctuation piling up
Auto-formatting tools like black and linters like flake8, which lean on indentation carrying real meaning

Recommended resourcesAffiliate links Β· may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice β€” pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from β€” great next to the visualizers.See the books

Practice what you learned

Re-implement each visualized algorithm in Python yourself β€” building beats watching.