Python Pass Statement | Use, Example

The pass statement in Python is a simple statement that tells the Python interpreter to ‘do nothing’.

The interpreter simply continues with the execution of program whenever the pass statement is executed.

This amazing feature makes it a good placeholder whenever Python needs to form an empty sub-statement block.

The pass statement is very useful when we are creating a program and want to hold the place of a specific code that is not ready or incomplete code.

In Python, the pass statement is also called a null statement or empty statement.

The major difference between pass and comment is that the interpreter ignores the comment entirely in the program, while it does not ignore pass. It outcomes in no operation (NOP).

Let’s take an example to understand the pass statement.

x = 20
y = 30
if x > y:
    pass
Output:
     No output

Syntax of Pass Statement in Python


The general syntax for a pass statement in Python programming language is as follows:

pass

Here, pass is a keyword in Python. Look at the below flowchart diagram of pass statement.

Flowchart Diagram:

Flowchart diagram of pass statement in Python

Use of Pass Statement in Python


In Python, we can use the pass statement in function definition def statement, if statement, or for or while loop where they must have a nonempty indented code block.

If the code block is missing, the syntax error will generate. For this reason, Python provides a pass statement, which does nothing, but it is still a valid statement.

There are the following uses of pass statement in Python that are as follows:

1. We can use the pass statement in the program when a statement is syntactically required, but we do not want to use any executable statement at its place.

2. We can use the pass statement to hold the place of code that is not ready or incomplete code yet. We often use in the place of loops or functions.

To use it, we just need to type the word ‘pass’ where we want to insert any other code (like loop or function). Let’s take an example to understand it better.

count = 0
while(count <= 5):
    pass

As you can see, we have created a while loop that is going to execute some code till the count is less than or equal to 5. Assume this is our code, but we have not complete writing the while loop and want to move on to something else and come back to it later.

This is the perfect time or place to use the pass statement in Python. Our code will do nothing. When writing the code is finished, we will place it at the place of pass statement.

3. Normally, we use the pass statement within the block of code under the loop statement, typically after a conditional if statement or while statement.

4. We mostly use the pass statement to create empty or null classes or functions. For example, the following below code defines a null function or null class:

def null_funct():
    pass

class null_class:
    pass

5. We can use pass statement while overriding a parent class method in the subclass but doesn’t have its specific implementation in the subclass.

Example Program based on Pass Statement


Example 1:

Let’s write a program to use the pass statement in ‘while loop’.

count = 0
while count <= 5:
    count = count + 1
    if count == 3:
        pass # nothing to be done on this line for output.
    print(count, end=' ')
Output:
      1 2 3 4 5 6

Example 2:

Let’s write a program in Python to use the pass statement in ‘for loop’.

for x in range(2, 6):
    if x == 4:
        pass
    print(x, end=' ')
Output:
      2 3 4 5

Example 3:

Let’s write a program to use the pass statement in creating an empty class.

class enpty_class:
    pass
Output:
      blank output

In some programming scenarios, we need to construct empty functions or classes. To construct an empty class, just type pass statement like above.

If we remove it, we will get an error: IndentationError: expected an indented block after class definition on line 1. Therefore, ‘pass’ statement is useful when a statement is syntactically required, but the program requires no action.


Example 4:

Let’s write a program in which we will print the value of n only if the value of n is odd.

for n in range(1, 11):
    if n % 2 == 0:
        pass # This statement does nothing for even number n.
    else:
        print(n, end=' ') # This statement prints odd number n only.
Output:
      1 3 5 7 9

In this example, if the value of n is even, the pass statement inside the if code block executes that does nothing. If you want to print both even and odd numbers, use the if-else statement inside the for loop.

But if you only want to print the odd numbers by checking the condition for even numbers, then use the Python Pass statement.

Example 5:

Let’s write a program in which we will print the current value from 6 to 1 with using the pass statement.

num = 6
while num > 0:
    if num == 3:
        pass
        print('Pass block executed')
    print('Current value: ',num)
    num = num - 1
Output:
      Current value:  6
      Current value:  5
      Current value:  4
      Pass block executed
      Current value:  3
      Current value:  2
      Current value:  1

Difference between Pass statement and Continue statement


In Python, the pass statement has no effect. When you create a method, function, class, or a loop block code and you are not thinking to implement just yet, use the pass statement. If the Python encounters the pass statement, then the Python interpreter will pass the program control to the next line of code.

On the other hand, the Python encounters the continue statement; it skips all the remaining statements inside the loop and returns the program control to the top and perform the next iteration.


In this tutorial, you have learned about the pass statement with the help of various example programs. Hope that you will have understood the basic concept of pass statement and practiced all example programs.
Thanks for reading!!!