LambdaLabTM
Computer Science · Class 11 · Empty Statements
Types of StatementsEmpty⏱️ 8 min read

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:

grade.py
marks = 92

if marks >= 90:

print('Done')
Output
  File "grade.py", line 5
    print('Done')
    ^
IndentationError: expected an indented block after 'if' statement on line 3

The 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.

Note
Look at which line it blames. The 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:

Key Takeaway
Every block must contain at least one statement. A header that ends in a colon is a promise that a block is coming, and Python holds you to it.

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:

grade.py
marks = 92

if marks >= 90:
    # work out the grade later
print('Done')
Output
  File "grade.py", line 5
    print('Done')
    ^
IndentationError: expected an indented block after 'if' statement on line 3

The 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.

🕳️ What can go in the block?

Three things you might write under the header. Only one of them is a statement.

grade.py
headerbody — the indented blockoutside
marks = 92
if marks >= 90:
nothing here
print('Done')
❌ nothing runs at all
  File "grade.py", line 5
    print('Done')
    ^
IndentationError: expected an indented block after 'if' statement on line 3

Nothing 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.

grade.py
marks = 92

if marks >= 90:
    pass
print('Done')
Output
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.

Note
Two names, one thing. Question papers call it the empty statement or the null statement; the keyword is 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:

HeaderNeeds a block?Left empty, you get
if …:yesIndentationError
elif …:yesIndentationError
else:yesIndentationError
for …:yesIndentationError
while …:yesIndentationError

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:

empty_loop.py
# a loop whose body has not been written yet
for i in range(1, 4):
    pass

print('The loop is over')
Output
The loop is over

6Recap

Key Takeaway
The empty statement is the fourth kind of statement. It exists because a block in Python cannot be left blank — every block needs at least one statement, and a comment is not one. The keyword is pass: Python counts it as a statement, and running it does nothing.
Note
The next lesson is about the situations you would actually reach for it in — because “a statement that does nothing” sounds useless right up until the first time you need one.
Quick Check

Why can a block in Python not be left blank?

Quick Check

A student fills an empty if block with the comment # do this later. What happens?

Quick Check

What does the empty statement do when the program reaches it?