Python · control flow

Pass Statement

A statement that does nothing, for when Python needs a block but you have nothing to put in it yet.

Why pass exists

Every if, loop, function and class needs an indented block. An empty block is a syntax error.


            if ready:
                # TODO
            print("next")
          
IndentationError: expected an indented block

            if ready:
                pass  # TODO
            print("next")
          
Valid: pass fills the block and does nothing

Pass in if


        # placeholder for an empty block
        if True:
            pass
        # ans: nothing happens
      

Pass in a function


        # empty function
        def my_function():
            pass
        # ans: the function exists, and does nothing when called
      

Pass in a class


        # empty class
        class MyClass:
            pass
        # ans: an empty class definition
      

Pass in a loop


        # pass in a loop
        for i in range(3):
            pass
        # ans: the loop runs 3 times and does nothing
      

Multiple pass


        # multiple pass statements
        if True:
            pass
            pass
        # ans: both do nothing
      

Exercises

Predict what happens, then press → to check.

Exercise 1


        # what happens?
        x = 5
        if x > 3:
            pass
        # ans: nothing (pass is a no-op)
      

Exercise 2


        # can you have an empty if?
        if True:
        # ans: SyntaxError (the block needs at least pass)
      

Python reports it as an IndentationError, which is a kind of SyntaxError.

Exercise 3


        # pass in a loop?
        for i in range(3):
            pass
        # ans: the loop runs 3 times and does nothing
      

Exercise 4


        # multiple pass?
        if True:
            pass
            pass
        # ans: valid, both do nothing
      

Exercise 5


        # pass vs continue?
        for i in range(3):
            pass
            print(i)
        # ans: 0, 1, 2 (pass doesn't skip anything)
      

continue would jump back to the loop and skip the print; pass just moves on to the next line.

Exercise 6


        # pass with else?
        if False:
            pass
        else:
            print("Else")
        # ans: Else
      

Exercise 7


        # what is x?
        x = 5
        if x > 10:
            x = 10
        else:
            pass
        # ans: x is still 5
      

Exercise 8


        # pass in try?
        try:
            x = 5
        except:
            pass
        # ans: valid, x is 5
      

Exercise 9


        # pass after return?
        def func():
            return 5
            pass
        # ans: valid, but pass is unreachable
      

return leaves the function, so nothing after it in the same block ever runs.

Exercise 10


        # empty except with pass?
        try:
            x = 1 / 0
        except:
            pass
        # ans: the error is silently caught
      

Silently swallowing every error hides bugs; use it sparingly.

Remember

A no-op

pass does nothing at all; execution continues with the next line.

A placeholder

Use it where a block is required but empty: if, loops, functions, classes, except.

Not continue

pass skips nothing; continue skips the rest of the loop body.