Python indentation and colons

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

Python indentation and colons

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which if statement is written correctly?
A if score > 10
B if score > 10:
2. Which code block is indented correctly?
A if age >= 13: print("Teen")
B if age >= 13: print("Teen")
3. How many spaces are normally used for each level of indent in Python?
A 4 spaces
B 1 space
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
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

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

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

Definitions

:

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
if age >= 13_ print("Teen")
2.
for i in range(3)_ print(i)
3.
_____ count < 3: print(count) count = count + 1
4.
def greet(name)_ print("Hi " + name) greet("Mia")
5.
for i in range(2): print(i) _____("Finished")

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A score = 8
B if score > 5:
C print('Pass')
D else
E print('Try again')
Question 2
A count = 0
B while count < 3:
C count = count + 1:
D print(count)
Question 3
A for i in range(5)
B print(i)
C print('End')
Question 4
A def greet(name):
B print('Hi', name)
C if name == 'Sam'
D print('Hello')
Question 5
A def show_score(score);
B print('Score:', score)
C show_score(10)

Answer Key

Python indentation and colons
Section A: Quick Questions
Q1
B — if score > 10:
Q2
A — if age >= 13: print("Teen")
Q3
A — 4 spaces
Q4
A — All 3 lines use the same amount of indent
Q5
B — Every time, because it is not indented
Section B: Match the Pairs
Q1
if score > 10: → The colon at the end of the line starts a block. The indented lines under it belong to the if.
Q2
print("Hi") → This line is indented 4 spaces, so it is inside the block above it.
Q3
Every line inside a block is indented by the same amount, usually 4 spaces → for i in range(3): print(i) print(i * 2)
Q4
The block ends when the indent ends, so the next unindented line runs after the block → while x < 3: x = x + 1 print("Done")
Q5
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.
Q6
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.
Section C: Definitions
Q1
: — 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.
Section D: Fill in the Blanks
Q1
if age >= 13: print("Teen")
Q2
for i in range(3): print(i)
Q3
while count < 3: print(count) count = count + 1
Q4
def greet(name): print("Hi " + name) greet("Mia")
Q5
for i in range(2): print(i) print("Finished")
Section E: Spot the Error
Q1
D — The else line starts a block, so it must end with a colon. It should be 'else:'.
Q2
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'.
Q3
A — A for line starts a block, so it must end with a colon. It should be 'for i in range(5):'.
Q4
C — An if line starts a new block, so it must end with a colon. It should be " if name == 'Sam':".
Q5
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):'.