Python pass Statement

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 !!

Was this post helpful?

Join 7000+ Fellow Programmers

Subscribe to get new post notifications, industry updates, best practices, and much more. Directly into your inbox, for free.

Leave a Comment

HowToDoInJava

A blog about Java and its related technologies, the best practices, algorithms, interview questions, scripting languages, and Python.