The pass statement is a special statement in Python that does nothing. It is used as a placeholder when a statement is required syntactically, but no action needs to be performed yet.
The pass statement tells Python to do nothing at that point.
pass
It is useful when you want to write the structure of a program first and add the actual code later.
if True:
pass
print("Program continues")
The pass statement does nothing, so Python continues with the next statement.
Python requires an indented statement inside structures such as if statements, loops, functions, and classes.
If you do not want to write any action yet, you can use pass.
if age >= 18:
pass
This is valid Python code.
age = 20
if age >= 18:
pass
print("Program continues")
The if condition is True, but pass tells Python to do nothing inside the if block.
age = 20
if age >= 18:
print("Adult")
else:
pass
for i in range(5):
if i == 2:
pass
print(i)
The pass statement does not skip the value 2. The print statement still executes.
i = 1
while i <= 5:
if i == 3:
pass
print(i)
i += 1
Using pass:
for i in range(5):
if i == 2:
pass
print(i)
Using continue:
for i in range(5):
if i == 2:
continue
print(i)
pass does nothing, while continue skips the current iteration.
Using pass:
for i in range(5):
if i == 2:
pass
print(i)
Using break:
for i in range(5):
if i == 2:
break
print(i)
The break statement stops the loop, while pass allows the loop to continue normally.
The pass statement can be used when creating a function whose implementation will be added later.
def calculate():
pass
print("Function created")
The function exists, but it does not perform any action.
The pass statement can also be used when defining an empty class.
class Student:
pass
student = Student()
print("Student object created")
def welcome():
pass
welcome()
print("Done")
The function executes but has no action because it contains only pass.
for i in range(5):
pass
print("Loop finished")
The loop runs, but its body performs no operation.
Sometimes developers create the structure of a program before implementing every feature.
def login():
pass
def register():
pass
def logout():
pass
Later, actual code can be added inside these functions.
marks = 75
if marks >= 40:
if marks >= 80:
print("Excellent")
else:
pass
else:
print("Fail")
Here, marks are between 40 and 79, so the inner else block executes pass and nothing is printed.
number = 10
if number > 0:
if number < 20:
pass
print("Completed")
The inner condition performs no action because it contains pass.
age = int(input("Enter age: "))
if age < 18:
pass
else:
print("Adult")
If the user enters 15, nothing is printed. If the user enters 20, the output is:
The pass statement can also be used inside an exception handler when you intentionally do not want to take any action.
try:
number = int("abc")
except ValueError:
pass
print("Program continues")
number = 10
if number > 0:
pass
elif number == 0:
print("Zero")
else:
print("Negative")
print("Done")
The first condition is True, so Python executes pass and then continues after the complete if-elif-else structure.
for i in range(3):
for j in range(3):
if i == j:
pass
print(i, j)
The pass statement does not affect the loop execution.
Consider this code:
if age >= 18:
This produces an indentation or syntax-related error because Python expects an indented statement after the colon.
Using pass solves this problem:
if age >= 18:
pass
| Statement | Purpose |
|---|---|
| pass | Does nothing. |
| continue | Skips the current iteration. |
| break | Stops the loop completely. |
Suppose we are designing a student management system and want to create functions first. We can use pass until their logic is implemented.
def add_student():
pass
def update_student():
pass
def delete_student():
pass
def view_student():
pass
Later, actual functionality can be added to each function.
Question: What does the pass statement do in Python?