The pass Statement
pass is the whole of the empty statement: one word, no brackets, no value, nothing after it. Running it does nothing. The interesting question is not what it does — you already know — but why anyone would write it on purpose. This lesson is the answer.
1How it is written
passThat is the entire syntax. It is a keyword, like if and for, so you cannot use the word pass as a variable name. It takes no condition and no value, and it is a single-word statement — it fills a line on its own.
pass under if marks >= 33: and reads it as “print Pass” has it exactly backwards: that block prints nothing at all. The word means pass over it — skip past, do nothing.2Use 1: the skeleton, before the logic exists
This is what pass is really for. When you plan a program, the shape arrives before the details — you know there will be three branches long before you know what any of them says. pass lets you write the shape down and still run the file.
Step through the four drafts. Each one fills in a branch, and each one runs:
Write one branch at a time. pass keeps the rest legal until you get there.
The shape of the whole program, and not one line of the work. It is a legal program — it runs, and it prints nothing whatever the marks are. Three pass statements are all that is holding the three blocks open.
Notice what draft 1 did: it printed nothing, for every mark. That is not a broken program — it is an unfinished one that works exactly as much as it has been written. You could hand it to a friend and it would not crash.
pass underneath each one, then running the file before you fill anything in. Any spelling mistake, missing colon or wrong indentation shows up immediately, while the program is still four lines long and easy to read.3The same trick inside a loop
A loop body can be left for later in exactly the same way. Here is a factorial program that has been planned but not written:
# the plan: multiply f by every number from 1 to num
num = 5
f = 1
for i in range(1, num + 1):
pass # the multiplying goes here
print(f)1
It runs, the loop goes round five times doing nothing, and f is still 1 at the end — the answer is wrong, but the program is alive and you can see it working. Now the body gets written and the pass goes:
# same program, with the body filled in
num = 5
f = 1
for i in range(1, num + 1):
f = f * i
print(f)120
pass is not part of the finished program. It held the block open until there was something to put in it, and then it was replaced. Finding a pass in code you are reading usually means somebody has not written this bit yet.4Use 2: a branch that is meant to do nothing
Sometimes the empty block is the finished answer. A temperature logger might want to shout about extreme readings and stay completely silent about normal ones:
# only the extremes are worth mentioning
reading = 25
if reading > 40:
print('Too hot!')
elif reading < 5:
print('Too cold!')
else:
pass
print('Reading recorded:', reading)Reading recorded: 25
else and its pass could both be deleted — the two lines together do nothing, and the program behaves identically without them. Some programmers keep it to say “I thought about this case and there is nothing to do”, which is a real message to the next reader. Others call it clutter. Both are defensible; what is not defensible is writing it because you think Python needs it. Here it does not.Where pass is genuinely needed is when the block would otherwise be the only thing in the statement — the skeletons above. If deleting the whole header along with it leaves the program you meant, you did not need either.
5pass vs a comment vs nothing
| In the block | Is it a statement? | The program |
|---|---|---|
nothing | no | IndentationError — nothing runs |
# a comment | no | IndentationError — nothing runs |
pass | yes | runs, and the block does nothing |
pass # note | yes | runs — the comment is just a note |
The last row is the one to copy. Write pass so the program runs, and put the note beside it so you remember what belongs there.
6Try it
Below is a skeleton that runs and does nothing. Replace each pass with a print(), one at a time, running it after every change — that is how the drafts above were made.
7You will meet it again
pass. That is where you will see it most often in real code.8Recap
pass is a keyword, a single-word statement, and the only empty statement Python has. It does nothing when it runs. Its job is to be a statement, so a block that has no work in it yet is still legal — which lets you write and run the shape of a program before you have written the program.An if block contains one line, and that line is pass. The condition is True. What is printed?
Which is the best reason to write pass?
A loop body contains only pass. How many times does the body run?