Python Tutorial - Pass Statement

Jinku Hu Jun 25, 2020
  1. Python pass Statement
  2. pass Statement Example
Python Tutorial - Pass Statement

In this section, you will learn Python pass statement.

Python pass Statement

pass is actually a null statement which is generally used as a placeholder. When you want to declare a function or a loop but not want to provide the implementation then you can use pass statement. It is similar to ; in C programming language or nop in assembly language.

The pass statement will result in no operation (NOP) which means nothing happens when pass is executed.

So what is the difference between pass and Python comments when nothing happens to both of them?

The comments are ignored and not executed, but pass statement will be executed resulting in nothing.

The following is the syntax of pass in Python:

pass

If you want to provide the implementation of a loop or function in the future then you have to use the pass statement because a function or a loop can never have an empty body in Python.

pass statement creates an empty body for you.

pass Statement Example

l = ["p", "y", "t", "h", "o", "n"]
for i in l:
    pass

So here, for loop has an empty body indicated by pass statement. If there is no pass statement and body of for is left empty you will have a SyntaxError - expected an indented block.

Similarly, pass can also be used in classes and functions when you plan to implement classes and function in the future. Consider the example below:

def function(args):
    pass
class ABC:
    pass
Author: Jinku Hu
Jinku Hu avatar Jinku Hu avatar

Founder of DelftStack.com. Jinku has worked in the robotics and automotive industries for over 8 years. He sharpened his coding skills when he needed to do the automatic testing, data collection from remote servers and report creation from the endurance test. He is from an electrical/electronics engineering background but has expanded his interest to embedded electronics, embedded programming and front-/back-end programming.

LinkedIn Facebook