Skip to content
Home » Programming » The pass statement in Python – Do Nothing

The pass statement in Python – Do Nothing

The pass statement in python

In python, pass is a keyword which acts as an entire statement and does nothing. Though pass does nothing, interpreter does not ignore it during the execution of the program. It does nothing particular but it acts as a placeholder. pass can be used by the programmers and maintainers that leaving the function, conditional statements, etc. doing nothing is well intended.

Syntax

pass

What is the Use of a pass Statement?

The pass statement is mainly used when we need a statement syntactically but we don’t need any action to be performed. Although, pass does nothing it is highly used during programming.

Let’s say we need a function factorial but currently we don’t want to implement or we don’t know it’s solution right now then we can use a pass statement. Lets see using a program example.

Following program gives an error since the python interpreter expects at least one statement after defining a function.

# gives error
def factorial():

print("Program to Calculate Factorial")

In this condition, we can use the pass statement.

# runs smoothly
def factorial():
    pass

print("Program to Calculate Factorial")

A pass statement can help to define function ahead of time. In this way, you can reference the function for now without any statements inside it without any error.

Since a pass statement is simply a null statement, we can also use it with conditional statements, loop, except, class, etc. Also, we can use it without any supporting statements.

The pass statement with if statement

num = 22

if num % 3 == 0:
    pass # will be implemented later
elif num % 5 == 0:
    pass # will be implemented later
else:
    print('Number is not divisible by 3 and 5')
    pass
    print("Provided number is", num)

Output

Number is not divisible by 3 and 5
Provided number is 22

Here, we want to show the use of the pass statement with the if statement. As we can see we have used three pass statements; one with if, another with elif and last one with else statement. When the condition is true for if and elif nothing happens because the interpreter does nothing when it encounters a pass statement. However, with the else statement we have used a print statement before and after the pass statement. In this condition, first print is executed then when pass is encountered nothing happens. After that, the next immediate statement, that is, another print statement is executed.

What is the difference between pass and continue in for loop?

Using continue skips the currently running cycle of the loop and starts another cycle of the loop if the condition is true. All statements after continue statements are skipped. However, pass does nothing when encountered; control is transferred to the next immediate statement.

We will use two programs one using pass and another using continue to show the difference between them.

To illustrate pass statement

# Example for pass in for loop
for value in range(7):
    if value % 3 == 0:
        pass
    print(value)

Output

0
1
2
3
4
5
6

To illustrate continue statement

# Example for continue in for loop
for value in range(7):
    if value % 3 == 0:
        continue
    print(value)

Output

1
2
4
5

Over here, you can see that all values are printed while using pass but not in continue. While using pass, when the condition is true then pass is executed and nothing happens. After that, control is transferred to the next immediate statement, that is, print statement. But while using a continue statement, when the condition is true then a continue statement is encountered which skips the current cycle, that is, all upcoming statements. So, values divisible by 3 are not printed.

Why does python keep evaluating code after pass?

Because pass is a null operation and does nothing. It does not block upcoming statements.

What is the Difference between pass and comment in python

When we write a comment in python, the interpreter completely ignores it and does not execute it at all. But when we write a pass statement it is not ignored at all, the interpreter executes it but does nothing.

Comment -> completely ignored; Isn't executed
pass -> is not ignored; executed but does nothing

More Examples

Leave a Reply

Your email address will not be published. Required fields are marked *