← Years 7–8 Creating Digital Solutions

Python indentation and colons

A colon at the end of a line starts a block. The lines inside the block are indented by the same amount (4 spaces), and the block ends when the indent ends.

Understanding Python indentation and colons in Years 7–8 Creating Digital Solutions

What it does: Both lines inside the loop block start with 4 spaces, so they line up and both belong to the loop. print("Done") is not indented, so it is outside the while block and runs only once the loop is finished. The line after a colon must be indented. Without the indent, Python gives an IndentationError. The standard in Python is 4 spaces for each level of indent. All lines in the same block must be indented by the same amount.

Common mistakes: The else line starts a block, so it must end with a colon. It should be 'else:'. Only a line that starts a block ends with a colon. This line is an ordinary instruction inside the loop, so it should have no colon: ' count = count + 1'.

Class: _____________________  ·  Name: _____________________  ·  Date: _________

Free printable worksheet with full answer key — or add this module to a class and your students practise it interactively.

This module's key terms are also in a printable crossword (Modules 50–125) and word search (Modules 3–125).

Take this worksheet further — free with Cloodit

Each concept is reinforced from every angle — learned, then applied, then tested.

Learning
Duo
Which if statement is written correctly?
if score > 10:if score > 10
Matches
if age >= 13 print("Teen")
↓
The if line is missing the colon at the end, so Python does not know a block is starting. Add : after the condition.
Applying
Meanings
Term
:
Meaning
A colon at the end of a line starts a block. The lines inside the block are indented by the same amount (4 spaces), and the block ends when the indent ends.
Practice
score = 12
Testing
Gaps
if age >= 13
Check Answer
Spots
3 print('Pass')
4else

Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.

The 'Starfish' badge — Five-Point Star, one of Cloodit's 106 collectible badges
Students earn collectable badges

Over 100 of them, across seven natural biomes.

Study Buddy
Students build a real study resource

Turns their work into revision material ready for Flash Cards and Exam study.

Diamond Dash — one of Cloodit's live, competitive Sprint games
Students lock in with Sprints

Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.

A Cloodit certificate of achievement, ready to personalise and print
Custom printable certificates

Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.

Quick Questions

1. Which if statement is written correctly?
A. if score > 10
B. if score > 10: ✓
A line that starts a block must end with a colon (:). Without it, Python shows a SyntaxError.
2. Which code block is indented correctly?
A. if age >= 13: print("Teen") ✓
B. if age >= 13: print("Teen")
The line after a colon must be indented. Without the indent, Python gives an IndentationError.
3. How many spaces are normally used for each level of indent in Python?
A. 4 spaces ✓
B. 1 space
The standard in Python is 4 spaces for each level of indent.
4. A for loop has 3 lines inside it. How should they be indented?
A. All 3 lines use the same amount of indent ✓
B. Each line uses a different amount of indent
All lines in the same block must be indented by the same amount.
5. Look at this code: if x > 5: print("Big") print("Done") When does "Done" print?
A. Only when x is bigger than 5
B. Every time, because it is not indented ✓
A block ends when the indent ends. The line print("Done") is outside the if block, so it always runs.

Match the Pairs

if score > 10: → The colon at the end of the line starts a block. The indented lines under it belong to the if.
print("Hi") → This line is indented 4 spaces, so it is inside the block above it.
Every line inside a block is indented by the same amount, usually 4 spaces → for i in range(3): print(i) print(i * 2)
The block ends when the indent ends, so the next unindented line runs after the block → while x < 3: x = x + 1 print("Done")
if age >= 13 print("Teen") → The if line is missing the colon at the end, so Python does not know a block is starting. Add : after the condition.
for i in range(3): print(i) → The line after the colon is not indented, so it is not inside the block. Indent it by 4 spaces.

Key Term

:
A colon at the end of a line starts a block. The lines inside the block are indented by the same amount (4 spaces), and the block ends when the indent ends.

Fill in the Blanks

1. if age >= 13: print("Teen")
Hint: Type the symbol that ends the if line and starts the indented block.
2. for i in range(3): print(i)
Hint: Type the punctuation that a for line must end with before its indented lines.
3. while count < 3: print(count) count = count + 1
Hint: Type the keyword that starts a loop block, with both indented lines inside it.
4. def greet(name): print("Hi " + name) greet("Mia")
Hint: Type the symbol that ends the function header line and opens the block.
5. for i in range(2): print(i) print("Finished")
Hint: Type the function name for the last line, which is not indented because the block has ended.

Spot the Error

Question 1
A score = 8
B if score > 5:
C print('Pass')
D else
E print('Try again')
Line D — The else line starts a block, so it must end with a colon. It should be 'else:'.
Question 2
A count = 0
B while count < 3:
C count = count + 1:
D print(count)
Line C — Only a line that starts a block ends with a colon. This line is an ordinary instruction inside the loop, so it should have no colon: ' count = count + 1'.
Question 3
A for i in range(5)
B print(i)
C print('End')
Line A — A for line starts a block, so it must end with a colon. It should be 'for i in range(5):'.
Question 4
A def greet(name):
B print('Hi', name)
C if name == 'Sam'
D print('Hello')
Line C — An if line starts a new block, so it must end with a colon. It should be " if name == 'Sam':".
Question 5
A def show_score(score);
B print('Score:', score)
C show_score(10)
Line A — A def line must end with a colon, not a semicolon. The colon starts the function block. It should be 'def show_score(score):'.

Student with a class code? Join your class →