Python Syntax, Indentation & Comments
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.
A colon opens a block
Keywords like if, for, while, def, and class end their line with a colon, signaling "an indented block follows."
The next lines must be indented
Python expects at least one indented line right after the colon β 4 spaces is the standard convention.
Same indentation, same block
Every line indented to the same level belongs to the same block, whether that block has one line or fifty.
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.
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.
age = 20if age >= 18: print("You can vote.") # inside the if-block print("Welcome, adult!") # still inside β same indentationprint("This always runs.") # back to column 0 β outside the if-blockRecommended resourcesAffiliate links Β· may earn us a commission
Recommended for learners
Practice what you learned
Re-implement each visualized algorithm in Python yourself β building beats watching.