A Python block is one or more statements grouped as the body of a control-flow construct. Indentation defines that grouping instead of braces, so the visual layout reflects execution structure. section 4.1 section 8.10
Use these rules when writing and reading blocks:
- Indent statements after a compound-statement clause such as if, while, or for. section 8.10
- Keep statements in the same block at the same indentation level; nested compound statements create nested blocks. section 8.10
- Prefer spaces and never mix tabs with spaces, because inconsistent indentation raises an exception. section 8.10
- Although blocks normally use separate lines, a single-line block or semicolon-separated statements are syntactically possible. section 8.10
Conditional blocks
x = 5 if x < 5: y = -1 z = 5 elif x > 5: y = 1 z = 11 else: y = 0 z = 10 print(x, y, z) # 5 0 10 The two statements under else belong to the same block because they share an indentation level. section 3.4.2
# This is Python code.
n = 9
r = 1
while n > 0:
r = r * n #1
n = n - 1 #2
Compare brace-delimited C with Python indentation while calculating a factorial; the equivalent control-flow body is visible directly in the layout. section 4.1




