Getting Started with Python · Lesson 6

Indentation and Blocks

Indentation is part of Python syntax.

ConceptWorked examplePracticeKnowledge check
Textbook walkthrough

Indentation and Blocks

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.

Deeper walkthrough

Read Indentation and Blocks as a mechanism, not a recipe

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.

Mechanism

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.

Evidence

Know what would convince you

  • Trace a tiny input by hand and compare the runtime result.
  • Inspect type, value/shape and any mutation/side effect explicitly.
Useful distinctionInput: Objects/values supplied to the operation.
How it works

Trace the mechanism step by step

  1. Use a colon to begin a compound statement.
  2. Indent the statements that belong to that block consistently, conventionally with four spaces.
  3. Dedent to return to the surrounding block.
  4. Avoid mixing tabs and spaces.
  5. When control flow looks wrong, inspect indentation before changing the condition itself.
Worked demonstration

Block structure

# 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")
Expected / illustrative result
Both indented statements run only when the condition is true; the final print is outside the if block.
Interpret the result.

For Indentation and Blocks, trace the specific input through the mechanism above and independently verify one returned value, state change or side effect.

Distinctions & related ideas

Place the concept correctly

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 deliberately

When it is appropriate

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.

Boundary conditions

When to stop or reconsider

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.

Common mistakes

Failure modes to recognise

  • Running the operation on the wrong object/type or in the wrong environment.
  • Inferring correctness from “no exception” without checking the produced value/state.
  • Hiding a boundary case instead of making its behaviour explicit.
Verification

How to check the result

  • Trace a tiny input by hand and compare the runtime result.
  • Inspect type, value/shape and any mutation/side effect explicitly.
  • Run an edge or invalid case and confirm the exception/behaviour is deliberate.
Hands-on practice

Demonstrate understanding

Try this:

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.

Use the smallest values that expose the language rule. Write the expected value and type first, then compare the actual state/output with that prediction.
Knowledge check

Check reasoning, not memorisation

Which approach best demonstrates understanding of Indentation and Blocks?

Quick reference

Remember the logic

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.
Lesson summary

What to remember

  • 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.
  • Use a colon to begin a compound statement.
  • Running the operation on the wrong object/type or in the wrong environment.
  • Trace a tiny input by hand and compare the runtime result.