Chapter 2 of 15All chapters
Chapter 2 of 15
Syntax and indentation
Indentation is the structure, not decoration.
Blocks are indented
Where other languages use braces, Python uses indentation. Everything indented under a line belongs to that line, and mixing tabs and spaces is an error rather than a style problem.
- Four spaces per level is the convention almost everyone follows.
- A colon ends the line that opens a block: if, for, while, def, class.
Comments and line endings
The # character starts a comment that runs to the end of the line. Statements end at the newline, so semicolons are legal but nobody uses them.