Online Tutorials & Training Materials | STechies.com
Register Login

How to Use the Pass Statement in Python?

26 Feb 2020 1:40 pm || 0

In this tutorial, you will learn the following:

What is Python Pass Statement?

The pass statement is a null operation; means nothing will happen when you execute the function. Although being a null statement, it is not ignored by the Python interpreter.

Use of Pass Statement in Python

In Python, we can use pass statements as place holdertoo. We use the pass statement as a place holder when we have to create a function that includes a fora statement but doesn’t want to implement some lines of code for now. This helps you to implement that code in the future. It is mostly used inside loop bodies as the for loops cannot have an empty body.

Pass statement is used when we want to declare an empty function or conditions or loops like if, for, while, etc.

Python Pass Statement

Let’s understand it more with the help of a few examples.

Example of Pass Statement with 'if statement'

Example:

# Python program to explain pass statement 
string1 = "Stechies"
# Pass Sting in for loop
for value in string1:
    if value == 'e':
        pass
    else:
        print("Value: ",value)

Output:

Value:  S
Value:  t
Value:  c
Value:  h
Value:  i
Value:  s

In the above example, for some reason, we don’t want to print the value of ‘e’. So we made an if-else condition under the for a loop. This if-else condition checks for the value of ‘e’ i.e if value == ‘e’ then execute the ‘pass’ statement or else print ‘value’.

Thus in the above code, whenever ‘e’ is encountered the pass statement is executed. And ‘e’ is not printed on the screen.

But what if we did not use the pass statement?

Python Pass Statement

Let us understand it with the help of an example.

Example:

string1 = "Stechies"
# Without pass statement
for value in string1:
    if value == 'e':
    else:
        print("Value: ",value)

Output:

D:\python>python pass.py
  File "pass.py", line 7
    else:
IndentationError: expected an indented block

In the above code, the pass statement is not used inside the if block. Which throws an indentation error. This is because python does not allow the declaration of empty function or condition and even loops. Here, there is nothing declared inside the if block thus the error.

To overcome this drawback in python, the pass statement is used.

1. Empty Function with Pass Statement

def abcfunction():
pass

2. Empty Function without Pass Statement

def abcfunction():

Output:

File "pass.py", line 2
^
SyntaxError: unexpected EOF while parsing

In the above examples, we defined 2 empty functions with and without the pass statement.

When we try to run the program without the pass statement it throws an error. This is because we cannot declare an empty function in python.

But there is often a time when there’s a need to declare an empty function for future reference. In that case, we can use the ‘pass’ statement. With the help of this statement, we can declare an empty function that won’t throw the error.

Can we use comments instead of pass?

We cannot use comments instead of a pass because the interpreter will ignore the comment and the pass statement returns null.

Pass VS Continue

Pass statement returns the null value and nothing else whereas,

The continue statement skips all the left statements and moves the control back to the top of the loop.

Let us understand it more with the help of an example

Example 1:

# Python program to explain pass statement
string1 = "Stechies"
# Pass String in for loop
for value in string1:
    print("Value: ",value)
    if value == 'e':
      pass
      print('This is pass block')
print("---------------------")

Output:

Value:  S
Value:  t
Value:  e
This is pass block
Value:  c
Value:  h
Value:  i
Value:  e
This is pass block
Value:  s
---------------------

Example 2:

# Python program to explain continue statement
string1 = "Stechies"
# Pass String in for loop
for value in string1:    
    if(value == 'e'):  
        print('This is continue block')
        continue      
print("Value: ",value)

Output:

Value:  S
Value:  t
This is continue block
Value:  c
Value:  h
Value:  i
This is continue block
Value:  s

In the above code, what 'continue' statement did was. It skipped the print statement whenever ‘e’ was encountered. But in the pass statement, ‘e’ was printed along with the statement ‘This is pass block’. As shown in the output below.

Pass VS Return

Let us understand it more with the help of an example.

When we use 'pass' in the function it would continue the execution of the code after the pass statement.

Whereas, when we use return in the function. It ends the execution of the function and control exists the body of the function.

Let us understand it with the help of an example.

Example 1:

def fun1():
    for i in 'hello':
         if(i == 'e'):
             pass
             print('pass executed')
         print(i)
fun1()
print("--------------------")

Output:

h
pass executed
e
l
l
o
--------------------

Example 2:

print("Return statement")
def fun2():
    for i in 'hello':
         if(i == 'e'):
             return
             print('pass executed')
         print(i)
fun2()
print("Outside the function fun2()")

Output:

Return statement
h
Outside the function fun2()

In the above code, when we used the return statement. It terminated the flow of the function and printed "Outside the function fun2()".

Conclusion

The greatest importance of the pass statement is the fact that it ensures the correctness of your code. Even if you are not writing some code for a specific function, you cannot leave the function blank, as this will throw an error. So, to prevent this, you have to use the pass statement.