The Python pass
statement is used to execute an empty statement. We can use the pass
statement when we do not want to execute any statement at a place in the code, but Python requires us to specify a statement to satisfy the Syntax rules.
Python pass
statement is different than a Python comment. A comment is totally ignored by the Python interpreter, but pass
sstatement is not ignored.
Python pass
Statement Example
The for loop in Python requires at least one statement to execute. If we do not write any statement under for loop body, the program will give an error.
If for some strange reason, we want to loop through a sequence but do not want to execute any statement, we can use pass
statement.
names = ["alex", "brian", "charles"] for x in names: pass print("Statement after the loop body")
Program output.
Statement after the loop body
The pass
Block
Please note that pass
statement does not work as the break statement. It does not pass the whole code block.
The pass
statement only passes the single statement. If there are other statements in scope of pass
block, they will be executed.
names = ["alex", "brian", "charles"] for x in names: pass print(x) print("Statement after the loop body")
Program output.
alex brian charles Statement after the loop body
Happy Learning !!
Leave a Reply