Pass Statement in Python

We use the pass statement when we want the loop to wait or do nothing when the condition is met. This statement will not skip or stop the loop, it is like telling Python to “pass” on that code.

Programmers use this statement when they don’t know or have the value to set a condition for a loop yet. It is like a placeholder and can be used with loops, functions, classes, etc.

Let us explain with some examples: We will first use the pass statement in a while loop:

number = 1
while number <= 10:

    if number == 6:
        pass
    print(number)  
    number += 1

Check out the result:

The pass simply told the computer, “You know what, do what you like.” The computer asks, “What should I do?” “pass”.

Let us use the pass statement in a for loop this time for the same code:

for number in range(1, 11):
if number == 6:

        pass
    print(number)

This will show the same result as above. Check it out and let me know using the comment section below.

Do you want to become an expert programmer? See books helping our students, interns, and beginners in software companies today (Click here)!

Leave a Comment

Python Tutorials