Follow the transformation
Use a colon to begin a compound statement.
Indent the statements that belong to that block consistently, conventionally with four spaces.
Dedent to return to the surrounding block.
Indentation is part of Python syntax.
Indentation is part of Python syntax. A colon after constructs such as if, for, while, def, class, try or with introduces an indented suite (block). Statements at the same indentation level belong to the same block, so changing indentation can change program logic rather than merely appearance.
Learning goal: explain why Indentation and Blocks behaves this way, apply it to a small example, and verify the result independently. Begin by being able to justify this first step: Use a colon to begin a compound statement.
Treat this as a sequence of observable decisions rather than one opaque command. Stage 1: Use a colon to begin a compound statement. Stage 2: Indent the statements that belong to that block consistently, conventionally with four spaces. Stage 3: Dedent to return to the surrounding block. Final checkpoint: When control flow looks wrong, inspect indentation before changing the condition itself.
Use a colon to begin a compound statement.
Indent the statements that belong to that block consistently, conventionally with four spaces.
Dedent to return to the surrounding block.
# Step 1 — Compute the right-hand expression and store its result in `score` for the next step.
score = 82
# Step 2 — Evaluate this condition and execute the indented branch only when the condition is true.
if score >= 50:
# Step 3 — Compute the right-hand expression and store its result in `result` for the next step.
result = "pass"
# Step 4 — Display the current value explicitly so the result/state can be inspected during execution.
print(result)
# Step 5 — Display the current value explicitly so the result/state can be inspected during execution.
print("finished")Both indented statements run only when the condition is true; the final print is outside the if block.
For Indentation and Blocks, trace the specific input through the mechanism above and independently verify one returned value, state change or side effect.
InputObjects/values supplied to the operation.StateNames or mutable objects that may change during execution.OutputReturned value, side effect, file, plot or exception to inspect.Use Indentation and Blocks when it answers a defined question in Getting Started with Python and its inputs/assumptions match the current data or program state.
Reconsider Indentation and Blocks when the required information is unavailable, the operation would violate a validation/data boundary, or a simpler operation answers the question more transparently.
Construct a tiny example of Indentation and Blocks. First use a colon to begin a compound statement. Then indent the statements that belong to that block consistently, conventionally with four spaces. Predict the result before execution and explain one boundary or failure case.
Which approach best demonstrates understanding of Indentation and Blocks?
Step 1Use a colon to begin a compound statement.Step 2Indent the statements that belong to that block consistently, conventionally with four spaces.Step 3Dedent to return to the surrounding block.Step 4Avoid mixing tabs and spaces.