A statement that does nothing, for when Python needs a block but you have nothing to put in it yet.
Every if, loop, function and class needs an indented block. An empty block is a syntax error.
if ready:
# TODO
print("next")
IndentationError: expected an indented block
if ready:
pass # TODO
print("next")
Valid: pass fills the block and does nothing
# placeholder for an empty block
if True:
pass
# ans: nothing happens
# empty function
def my_function():
pass
# ans: the function exists, and does nothing when called
# empty class
class MyClass:
pass
# ans: an empty class definition
# pass in a loop
for i in range(3):
pass
# ans: the loop runs 3 times and does nothing
# multiple pass statements
if True:
pass
pass
# ans: both do nothing
Predict what happens, then press → to check.
# what happens?
x = 5
if x > 3:
pass
# ans: nothing (pass is a no-op)
# can you have an empty if?
if True:
# ans: SyntaxError (the block needs at least pass)
Python reports it as an IndentationError, which is a kind of SyntaxError.
# pass in a loop?
for i in range(3):
pass
# ans: the loop runs 3 times and does nothing
# multiple pass?
if True:
pass
pass
# ans: valid, both do nothing
# pass vs continue?
for i in range(3):
pass
print(i)
# ans: 0, 1, 2 (pass doesn't skip anything)
continue would jump back to the loop and skip the print; pass just moves on to the next line.
# pass with else?
if False:
pass
else:
print("Else")
# ans: Else
# what is x?
x = 5
if x > 10:
x = 10
else:
pass
# ans: x is still 5
# pass in try?
try:
x = 5
except:
pass
# ans: valid, x is 5
# pass after return?
def func():
return 5
pass
# ans: valid, but pass is unreachable
return leaves the function, so nothing after it in the same block ever runs.
# empty except with pass?
try:
x = 1 / 0
except:
pass
# ans: the error is silently caught
Silently swallowing every error hides bugs; use it sparingly.
pass does nothing at all; execution continues with the next line.
Use it where a block is required but empty: if, loops, functions, classes, except.
continuepass skips nothing; continue skips the rest of the loop body.