Pass Statement in Python
The pass
statement in Python is a placeholder. It does nothing when
executed, but it's used when a statement is syntactically required.
It is useful when we write code structures like loops, functions, or conditions but don't want to run any code inside them *yet*.
Syntax:
if condition:
pass
# or inside loops or functions
for item in list:
pass
Note: Without pass
, Python will give an error if a block is left
empty.
Example 1: Using pass in if-statement
x = 5
if x > 0:
pass
print("Check completed.")
Output:
Check completed.
Explanation:
- The
if
conditionx > 0
is true. - Instead of running any code, we use
pass
as a placeholder. - The program continues and prints
"Check completed."
Example 2: Using pass inside a loop (reserved space)
for i in range(3):
pass
print("Loop ended")
Output:
Loop ended
Explanation:
- This loop runs 3 times, but
pass
means "do nothing" for each iteration. - No output appears from the loop, but the program still runs correctly.
- It then prints
"Loop ended"
.
Summary:
- pass means “do nothing”.
- Used as a placeholder where a block of code is required but not ready yet.
- Prevents errors in empty loops, functions, or conditions.