Javaexercise.com

What is the Pass Statement in Python?

Python is a high-level, interpreted programming language, and is one of the most popular programming languages present in the software industry. One of the most important but often overlooked concepts in python is that of the pass statement. Let us have a look at them in detail.

What are pass statements in Python?

In python, pass statements basically act as a form of null statements. They are similar to comments in python in many ways but one striking difference between the two is that comments are ignored by the compiler whereas pass statements are not ignored by the compiler.

At the first glance, the pass statement might seem to be sort of redundant or even useless at times but upon thinking, you might realize the advantages of the pass statement, especially for developers.

Developers usually use pass statements as a kind of placeholder for code that would be added at a later stage of development. This acts as some sort of a workaround in cases where empty code is not allowed and can be used to avoid errors. These cases include if statements, loops (for and while), and functions. 

Syntax

The syntax of pass statements is very straightforward and is as follows. We simply use the keyword pass. 

pass

Pass statement with if statement in Python

Pass statements can be used in the body of an if statement as well.

In this example, we shall consider a variable i which is assigned an initial value of 1.

The main purpose of the body of the if statement might still be undecided.

 Now let us say we want to keep the second if statement as it is but do not know at this time what we need to add to the body. For such cases, we use a pass statement.

The first if statement executes but the second if body passes the control to the next statement. Such a body of an if statement will not produce any output.

i=1

if(i>0):
    print(i)
if i<3: 
    pass # will not execute

Output

1

Pass statement with while loop in Python

Pass statements can be used in the body of a while loop as well.

In this example, we shall consider a variable i which is assigned an initial value of 1.

The condition for the while loop to keep executing is i<3 which means that as long as the value of i is less than 3, the body of the while loop will keep getting executed.

Now let us say we want to keep the while loop as it is but do not know at this time what we need to add to the body.

For such cases, we use a pass statement. Such a loop will not produce any output. 

i=1
print(i)
while i<3:
    pass # will not execute

Output

1

Pass statement with for loop in Python

Pass statements can be used in the body of a for loop as well.

In this example, we shall consider the for loop to run from 0 to 2 which is specified using the range() function with a parameter 3.

The main purpose of the body of the for loop is still undecided.

Now let us say we want to keep the for loop but do not know exactly what should be added to the body of the for loop.

For such cases, we use a pass statement. Such a loop will not produce any output.

i=1
print(i)
for i in range(3):
    pass # will not execute

Output

1

Pass statement with function in Python

Pass statements can be used in the body of a function as well.

Here, we shall consider a function and the main purpose of the body of the function is still undecided.

Now let us say we want to keep the first function as a part of our code but do not know at this time what we should add to the body of the function.

For such cases, we use a pass statement. Such a function will not produce any output. 

Although we call the function but did not get any output due to the pass statement. 

a = 10
b = 20

def getResult(a,b):
    pass # will not execute

def getSum(a,b):
    print("Sum = ",a+b)
    
getResult(a,b)
getSum(a,b)

Output

Sum = 30

Conclusion

In this topic, we have learned the use and advantages of pass statements in a Python program, following a running example of a simple iterative printing program, thus giving us an intuition of how this concept could be applied in real-world situations. Feel free to reach out to info.javaexercise@gmail.com in case of any suggestions.