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.
pass statement is different than a Python comment. A comment is totally ignored by the Python interpreter, but
pass sstatement is not ignored.
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
names = ["alex", "brian", "charles"] for x in names: pass print("Statement after the loop body")
Statement after the loop body
Please note that
pass statement does not work as the break statement. It does not pass the whole code block.
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")
alex brian charles Statement after the loop body
Happy Learning !!
Leave a Reply