In this tutorial, you’ll learn how to use the Python pass statement as a placeholder.
Consider the following if/else condition:
counter = 1 max = 10 if counter <= max: counter += 1 else: # implement later
You don’t have any code in the else clause yet. This else clause, however, will be coded later.
If you run the code in this situation, you’ll get a syntax error (SyntaxError).
The Python pass statement is useful in this situation:
counter = 1 max = 10 if counter <= max: counter += 1 else: pass
The statement pass has no effect. It’s only a placeholder for future code.
The Python interpreter will regard the pass statement as a single statement when you run the code that contains it. As a result, no syntax errors are generated.
In Python, the pass statement can technically be used in several statements.
Take a look at these pass statement examples.
if statement and pass in Python
The following code demonstrates how to use the pass statement in conjunction with an if statement:
if condition: pass
for loop and pass in Python
In this example, the pass statement is used in a for loop:
for i in range(1,100): pass
while loop and pass in Python
The pass statement is demonstrated with a while loop in the following example:
while condition: pass