Python course menu
Python pass Statement: A Placeholder That Does Nothing
Learn the Python pass statement: why Python needs a placeholder for empty blocks, using pass in functions, classes, loops and conditions, the ... (Ellipsis) alternative, and how pass differs from continue and break.
On this page
pass is a statement that does nothing. That sounds pointless, but Python's syntax requires every block (after if, for, def, class, …) to contain at least one statement. pass fills that space when you have nothing to put there yet.
Why Python needs it#
An empty block is a syntax error:
def todo():
print("after") File "example.py", line 2
print("after")
^^^^^
IndentationError: expected an indented block after function definition on line 1pass as a placeholder#
Sketch the structure of a program first, and fill in the details later:
def calculate_tax(amount):
pass
print(calculate_tax(100))NoneThe function exists and can be called; it just does nothing yet, so it returns None.
In a class#
An empty class is useful as a starting point, or as a simple custom exception:
class Invoice:
pass
inv = Invoice()
print(type(inv).__name__)InvoiceIn conditions#
Sometimes one branch genuinely needs no action, but writing it out makes the logic clearer:
value = 5
if value > 0:
pass # TODO: handle positive values
else:
print("not positive")
print("end")endA # TODO comment next to pass reminds you, and others, that the block is unfinished.
In loops#
pass inside a loop does nothing on each iteration, but the loop still runs completely:
for n in range(3):
pass
print("loop finished, n =", n)loop finished, n = 2pass vs continue vs break#
The Ellipsis alternative#
You will also see ... (three dots, called Ellipsis) used as a placeholder, especially in type stubs and documentation:
def f():
...
print(f())NoneBoth work. pass is the traditional choice in regular code.
Common mistakes#
Practice#
Practice exercise
Sketch a small program structure with three empty functions: load_data(), clean_data(data) and save_report(data). Call all three in order without errors.
Show hint
Each function body is just pass. Calling a function that only contains pass returns None.
Show solution
def load_data():
pass # TODO: read the CSV file
def clean_data(data):
pass # TODO: remove empty rows
def save_report(data):
pass # TODO: write the summary
data = load_data()
data = clean_data(data)
save_report(data)
print("Pipeline ran")Pipeline ranThis "skeleton first" approach lets you check the overall flow before writing each part.
Key takeaway#
Finished this lesson?