Python Pass Statement with Example

Python pass statement; Through this tutorial, i am going to show you what is pass statement in python and how to use pass statement with loops, if-statement.

Pass Statement in Python

  • What is Pass Statement in Python
  • Syntax of pass statement
  • Example: pass statement in python
  • Example: without pass statement in python function
  • Difference Between pass And continue Statement in Python

What is Pass Statement in Python

Pass statement is used to do nothing. It can be used inside a loop or if statement to represent no operation. Pass is useful when we need statements syntactically correct but we do not want to do any operation.

Syntax of pass statement

pass

Example: pass statement in python

#Check for each number generated by range function
for number in range(1, 10):
      #check if the number is even
      if number % 2 == 0:
          #if even, then pass ( No operation )
          pass
      else:
      #print the odd numbers
        print (number),

Output

 1
 3
 5
 7
 9

Explanation of above program

In the above given program, we used the range () function with the for loop. Here the range function will generate numbers from 1 to 10.

This program will check even and odd numbers. When even the number comes in the test condition, pass the pass statement and then go ahead to the next execution.

In this list, we have printed only Od number.

To remember about pass statement

  • If you are executing any program with for loop or have created a function. Or you have not written any code inside them. And if you run your program, it will throw an error.
  • And if you have passed a statement inside them, then it will not throw an error.

Example: without pass statement in python function

Let’s create a function and place it blank and do not put any code inside it.

def myFunc(arg): 

When you run this function, it will throw an error. If you had written a statement inside it, it would not have thrown an error.

You can see the error looks like:

File "", line 1
     def myFunc(arg):
                     ^
 SyntaxError: unexpected EOF while parsing

If you pass the pass statement inside the function as given below, then no error will occur.

You can write the pass statement inside the function like this:

def myFunc(arg): 
    pass

Difference Between pass And continue Statement in Python

Pass statement: You must have understood from the definition and examples given above that the pass statement simply does nothing. When you create a method, function, class, test condition, you use a pass statement that you don’t want to implement yet.

Continue statement: In the previous post, we have discussed the definition and issue of continuation statement. Where python continue statement skip all the the remaining statements in the loop and returns the control to the top of the loop. if you want to know more about continue statement click here.

Recommended Python Tutorials

Recommended:-Python Data Types

Leave a Comment