The pass statement is a null operation.
It is used when a statement is required syntactically, but you do not want to execute any code.
The pass statement allows you to create empty code blocks without causing an error.
Syntax
pass
Why Use Pass?
Python does not allow empty code blocks.
Example:
if True:
Output:
IndentationError: expected an indented block
To avoid this error, use pass.
Example:
if True:
pass
Pass in an If Statement
Example:
age = 18
if age >= 18:
pass
print("Program continues")
Output:
Program continues
Pass in a For Loop
Example:
for i in range(5):
pass
print("Loop completed")
Output:
Loop completed
Pass in a While Loop
Example:
count = 0
while count < 5:
count += 1
pass
print("Done")
Output:
Done
Pass in a Function
You can create an empty function and implement it later.
Example:
def my_function():
pass
This function does nothing but does not cause an error.
Pass in a Class
You can create an empty class using pass.
Example:
class Person:
pass
person = Person()
Practical Example
Create a placeholder for future code.
Example:
def login():
pass
def register():
print("Register Function")
Output:
No output
The login() function can be implemented later.
Pass vs Continue
pass does nothing.
Example:
for i in range(3):
pass
print("Finished")
Output:
Finished
continue skips the current iteration.
Example:
for i in range(3):
if i == 1:
continue
print(i)
Output:
0
2
Pass vs Break
pass does nothing and allows the loop to continue.
Example:
for i in range(3):
pass
print("Done")
Output:
Done
break exits the loop completely.
Example:
for i in range(3):
break
print("Done")
Output:
Done
The loop stops immediately after the first iteration.
Common Use Cases
- Creating placeholder functions
- Creating placeholder classes
- Writing incomplete code
- Avoiding syntax errors during development
Summary
- The
passstatement does nothing. - It is used as a placeholder for future code.
- Python requires code blocks to contain at least one statement.
passprevents syntax errors in empty blocks.- It can be used with
if,for,while, functions, and classes.