Pass Statement in Python with Example

Pass Statement in Python with Example

In this post, you will learn everything like about the pass statement in python with examples. We will explain like, what is pass statement, how to use pass statement with loops, if-statement.

Also, we will explain the 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

Flowchart of difference between pass and continue statement in python

difference between pass and continue statement in python

AuthorAdmin

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Python, Javascript, JQuery, Laravel, Livewire, Codeigniter, Node JS, Express JS, Vue JS, Angular JS, React Js, MySQL, MongoDB, REST APIs, Windows, Xampp, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL and Bootstrap from a starting stage. As well as demo example.

Leave a Reply

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