Featureintro
Statements and suites
Also called Python statements, simple statements, compound statements, suite, clause, block, semicolon.
A Python statement is simple (one logical line) or compound (clauses whose headers end in a colon). A suite is an indented block, or simple statements on the header line.
- Language
- Python
Synopsis
for n in range(3):
if n % 2 == 0:
print(n, "even")
else:
print(n, "odd")
total = 0; total += 5; print(total)
0 even
1 odd
2 even
5
Semantics
r1. "A simple statement is comprised within a single logical line." Several simple statements may share a line, separated by semicolons.1 Example: suites.
r2. "Compound statements contain (groups of) other statements". They include if, while, for, try, with, match, and function and class definitions.2 Example: suites.
r3. A compound statement consists of one or more clauses. A clause has a header and a suite; the headers of one statement are all at the same indentation level, and each begins with a keyword and ends with a colon.2 Example: suites.
r4. A suite is either semicolon-separated simple statements on the header's line, after the colon, or indented statements on the following lines. "Only the latter form of a suite can contain nested compound statements".2 Example: nested-one-line.
r5. The reference notes that "the semicolon binds tighter than the colon in this context", so in if x < y < z: print(x); print(y); print(z) either all or none of the calls run.2 Example: semicolon-binds.
r6. Optional continuation clauses such as else begin with a keyword that cannot start a statement, and nested if statements must be indented, which removes the dangling else problem.2 Example: suites.
r7. A suite cannot be empty. pass "is a null operation" for places where a statement is required syntactically but no code needs to run.3 Example: pass-placeholder.
r8. PEP 8 says that "Compound statements (multiple statements on the same line) are generally discouraged".4 Example: one-line-suite.
Examples
One-line suites and a trailing semicolon (rules r4 and r5). The grammar allows a final ; in a statement list.2
x, y, z = 1, 2, 3
if x < y < z: print(x); print(y); print(z)
print("end");
1
2
3
end
Placeholders for suites with nothing to do yet (rule r7).
class Empty:
pass
def todo():
...
for _ in range(3):
pass
print(Empty.__name__, todo())
Empty None
... works too: it is an expression statement that evaluates the Ellipsis constant.
Pitfalls
A one-line suite takes the whole line
Everything after the colon belongs to the suite (rule r5), so the second print is skipped too.
This code runs but gives a wrong answer on purpose.
ready = False
if ready: print("start"); print("this is skipped too")
print("after")
after
No nested compound statement on one line
A one-line suite holds only simple statements (rule r4).
This code raises SyntaxError, on purpose.
x = 1
if x: if x > 0: print("positive")
An empty suite
A header must be followed by at least one statement (rule r7). Without an indented line the parser raises IndentationError.
This code raises IndentationError, on purpose.
for n in range(3):
print(n)
Comparison
In Go, "A block is a possibly empty sequence of declarations and statements within matching brace brackets."5 An if statement always takes a block, and else is followed by another if or a block.6 Go marks a suite with braces and can leave it empty; Python marks it with the colon and indentation, and needs pass for an empty one (rule r7).
References
- The Python Language Reference, Python 3.15, 7 Simple statements
- The Python Language Reference, Python 3.15, 8 Compound statements
- The Python Language Reference, Python 3.15, 7.4 The pass statement
- PEP 8: Style Guide for Python Code, Other Recommendations
- The Go Programming Language Specification, Blocks
- The Go Programming Language Specification, If statements
About this page
The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H0 no proof |
|
| Lean | L0 no Lean part |
|
| Visual | V0 none |
|
| Writing | W3 standard |
|
Tag WA755V Edit this page History Report an error Markdown