What is an Empty Statement?
Every statement you have written so far was written because you wanted something to happen. The empty statement is the opposite: it is there to make nothing happen. That sounds like a waste of a keyword until you meet the situation it exists for — a block that Python insists on and you have nothing to put in.
1A program with a hole in it
You are writing a program that prints a grade. You know the shape of it already — a condition and a block underneath — but you have not decided what the message should say yet, so you leave the block for tomorrow:
marks = 92
if marks >= 90:
print('Done') File "grade.py", line 5
print('Done')
^
IndentationError: expected an indented block after 'if' statement on line 3The program does not run. Not the if, not the print, not the first line — nothing. This is a syntax error, so Python refuses the whole file before it starts.
if is on line 3, but the error is reported at line 5 — the first line that is not indented. That is the moment Python gives up waiting for the block.2Why Python refuses
Other languages put a block inside curly brackets, so an empty block is easy to write: { } and you are done. Python has no brackets. A block is the indented lines under a header, which means a block with nothing in it is not an empty block — it is no block at all.
So Python has a rule, and it is short:
3A comment does not count
The obvious repair is to write a note to yourself in the block. It looks filled now. It is not:
marks = 92
if marks >= 90:
# work out the grade later
print('Done') File "grade.py", line 5
print('Done')
^
IndentationError: expected an indented block after 'if' statement on line 3The same error, word for word. Comments are thrown away before Python looks for statements — you met that in the Comments lesson — so the block it sees is exactly as empty as it was before. The block looks occupied to you and is still empty to Python.
Try all three fillings below and watch which one Python accepts. Only the last one is a statement.
Three things you might write under the header. Only one of them is a statement.
File "grade.py", line 5
print('Done')
^
IndentationError: expected an indented block after 'if' statement on line 3Nothing is indented under the header, so Python reaches print('Done') while it is still waiting for the block. Note the line number it complains about: line 5, the first line that is not indented — that is the moment Python realises the block is never coming.
4The statement that does nothing
Python solves this with one word: pass. It is a real statement — Python counts it, so the block is properly filled — and when the program reaches it, it does nothing at all and moves on.
marks = 92
if marks >= 90:
pass
print('Done')Done
The block runs. Nothing happens inside it. The program carries on. That is the whole behaviour of the empty statement, and pass is the only way to write one.
pass. If a question asks for “the empty statement in Python”, the answer is pass.5Everywhere a block is needed
The if above is not a special case. Every header you have learned ends in a colon and demands a block, so every one of them can leave you with the same hole:
| Header | Needs a block? | Left empty, you get |
|---|---|---|
if …: | yes | IndentationError |
elif …: | yes | IndentationError |
else: | yes | IndentationError |
for …: | yes | IndentationError |
while …: | yes | IndentationError |
A loop is just as fussy as an if. This one repeats three times and does nothing three times, which is legal and occasionally exactly what you want:
# a loop whose body has not been written yet
for i in range(1, 4):
pass
print('The loop is over')The loop is over
6Recap
pass: Python counts it as a statement, and running it does nothing.Why can a block in Python not be left blank?
A student fills an empty if block with the comment # do this later. What happens?
What does the empty statement do when the program reaches it?