Python Tutorial Python Advanced Python References Python Libraries

Python pass Keyword



The Python pass keyword is used as a placeholder for future code. Python does not allow to create conditional statements, loops, functions or classes with empty codes and raises exception if created with empty codes. Instead of empty codes, pass keyword can be used to avoid such errors.

Example:

In the example below, empty if statement is created, which raises exception upon execution.

MyString = "Python"
if (MyString == "Python"):

print("pass statement allows to create objects with empty codes.")

The output of the above code will be:

  File "Main.py", line 4
    print("pass statement allows to create objects with empty codes.")
    ^
IndentationError: expected an indented block

Example:

When pass statement is used instead of empty if statement, no exception will be raised and program will execute next line of codes.

MyString = "Python"
if (MyString == "Python"):
  pass

print("pass statement allows to create objects with empty codes.")

The output of the above code will be:

pass statement allows to create objects with empty codes.

Example:

Similarly, the pass statement can be used to create empty loops, functions or classes.

for i in range(1, 10):
  pass

def MyFunction():
  pass

class MyClass:
  pass

print("pass statement allows to create objects with empty codes.")

The output of the above code will be:

pass statement allows to create objects with empty codes.

❮ Python Keywords