How to use Python pass

Python pass is a placeholder which prevents the program from issuing an error message. The statement protects the syntax.

What is Python pass?

Python pass acts primarily as a placeholder without any other basic functions. In some cases, it may be important to include a statement in the code to instruct the program to continue running. This prevents problems in Python. A typical example would be when a code has an empty space which has been created for a function, but it has not developed yet. An error message would pop up here if the body was left empty. Python pass prevents this. The placeholder can be replaced by the actual function later on and the code will remain correct.

How is Python pass used?

Python pass uses the pass command and is placed within the code block. Python pass is used primarily in loops, where a certain function may not have been defined yet. The placeholder is often used after an “if” statement. It will look like this:

if some_condition:
    print("Condition encountered")
else:
    pass

Python pass can also be combined with other statements.

How does Python pass work?

Python pass working in a loop is somewhat similar to the police directing traffic after an accident. The program begins the first pass, queries a defined condition, checks whether it is true or false, and proceeds accordingly. However, if the developer did not include a consequence, the program would issue an error sign here. Python pass would then steer the program past the “accident point” and ensure that the query continues to run without interruption.

Python pass in a “for” loop

The right examples illustrate how Python pass is used and highlights statement’s advantages. A number series will be created with a Python “for” loop in the first example. The program keeps counting up from 0. The termination condition states that the number should not become greater than or equal to 10. You would normally insert a restriction once the counter reaches 8 using a Python “if else” statement. However, Python pass removes this restriction and tells the program to simply continue.

for num in range(10):
    if num == 8:
        pass
    print(f"The current number is: {num}")
print("The count is finished")

The output now reads as follows:

The current number is: 0
The current number is: 1
The current number is: 2
The current number is: 3
The current number is: 4
The current number is: 5
The current number is: 6
The current number is: 7
The current number is: 8
The current number is: 9
The count is finished

The output looks like a loop that just ran through normally and counted from 0 to 9 without interruption. However, if you had left out the pass statement at this point, the “if” statement would have been incomplete and the program would have issued an error. So it’s not like Python pass is being ignored, rather the statement is simply telling the program to ignore the current incomplete line and focus on upcoming statements.

Python pass in a “while” loop

Python pass can also be used in a simple Python “while” loop which you may already know from our Python tutorial. In the following example, the loop checks if a number is less than 5. Since the consequent (else) is not yet defined, the program would detect an error if the number is equal to or greater than 5, and then issue an error. However, Python pass allows the program to just run through.

counter = 0
while counter < 5:
    print(f"The number {counter} is less than 5")
    counter = counter + 1
else:
    pass

Python pass in a class

Additionally, Python pass also works inside a class of the internet programming language. This is useful if you have already created the class and do not know all of its contents.

In the following example, there is a distinguishment between vehicles driving into a yard. There are already concrete instructions for cars and trucks, but not for bicycles. This looks like this:

class vehicle:
    def car(self):
        print("A car has driven into the yard")
    def truck(self):
        print("A truck has driven into the yard")
    def bike(self):
        pass

The differences to break and continue

While Python pass is similar to the break and continue statements, there are also some very crucial differences. All three statements are designed to prevent a loop or program from making errors. However, while Python pass makes the program run properly, break and continue intervene when needed. The break statement causes a loop to terminate completely if a condition is not met before it ends. The continue statement causes a loop to be interrupted at a certain external event and to continue afterwards. You can read more on this in our article on Python break and continue.

Unnecessary use of Python pass

Python pass is unnecessary in some cases because the statement basically asks the program to do nothing. It is not necessarily wrong in this case, but the result would be identical and the code shorter if Python pass were left out. Below is an example of this kind of case:

if 2 + 2 == 4:
print("This result is correct")
    pass
    print("This was to be expected")

If a program runs through this code, the output would be:

This result is correct
This was to be expected

The result would be the same without the pass statement, but your code would be shorter and more concise. Python pass does not need to be used very often. Try to only use it in places that would be prone to errors.

We use cookies on our website to provide you with the best possible user experience. By continuing to use our website or services, you agree to their use. More Information.