Cookie Policy

We use cookies to operate this website, improve usability, personalize your experience, and improve our marketing. Privacy Policy.

By clicking "Accept" or further use of this website, you agree to allow cookies.

Accept
Learn Machine Learning by Doing Learn Now
You are reading solutions / Python
alfie-grace-headshot-square2.jpg
Author: Alfie Grace
Data Scientist

Python pass statement: When, why, and how to use it

LearnDataSci is reader-supported. When you purchase through links on our site, earned commissions help support our team of writers, researchers, and designers at no extra cost to you.

What does pass do?

The pass statement does nothing in Python, which is helpful for using as a placeholder in if statement branches, functions, and classes. In layman's terms, pass tells Python to skip this line and do nothing.

To demonstrate how pass works, let's write a small script to iterate through the numbers from one to seventy and then print out any multiples of seven.

for number in range(1, 71):
    if number % 7 != 0:
        # the number is not a multiple of 7
        pass
    else:
        print(f'{number} is a multiple of 7')
Out:
7 is a multiple of 7
14 is a multiple of 7
21 is a multiple of 7
28 is a multiple of 7
35 is a multiple of 7
42 is a multiple of 7
49 is a multiple of 7
56 is a multiple of 7
63 is a multiple of 7
70 is a multiple of 7

pass is a control statement

if, while and for statements are fundamental to all Python programs. These statements can be influenced by what are known as control statements, allowing you to govern your code in different ways. The three control statements in Python are pass, continue and break.

This article looks specifically at the pass statement.

Why use pass?

As mentioned previously, pass is usually used as a placeholder for branches, functions, classes. Whenever Python arrives at a pass statement, it passes straight over it (hence the name).

This functionality may seem pointless, but let's try and run our example from the introduction again, without the pass statement:

for number in range(1, 71):
    if number % 7 != 0:
        # the number is not a multiple of 7
    else:
        print(f'{number} is a multiple of 7')
Out:
File "<ipython-input-2-793ee23f6307>", line 5
    else:
       ^
IndentationError: expected an indented block

This code gives us IndentationError: expected an indented block which we're getting because we haven't added anything to our if statement.

A codeless statement like this is known as an empty suite. We can avoid this error by using pass as a placeholder for our elif statement as seen in the introduction.

The addition of pass means that Python does nothing in situations where the number variable isn't a multiple of 27. Despite this seeming redundant, it prevents Python from throwing an error. The flowchart shown below helps demonstrate how pass is working here:

If pass doesn't do anything, why bother using it at all? To avoid getting an empty suite error, you could delete the code block causing the problem, removing the need for a pass statement.

When writing scripts in Python, there are often situations where you'll need to code the general layout of a script first, then come back to fill in the blanks later. Using the pass statement in situations like this lets you focus on the structure by writing the code branches first and then using pass as a placeholder to prevent errors from occurring, allowing you to test how the overall script is working before worrying about more minor details.

More examples

Usage in functions

Another example of how pass can be helpful like this is when writing functions. Let's say we'd like to write a script that takes two user-input numbers then performs some calculations on them. We could write a function perform_calculations to handle the math.

Before filling out the function, we'd like to test if our user inputs are getting recorded correctly:

def perform_calculations(a, b):
    # add functionality later

a = int(input('Enter the first number: '))
b = int(input('Enter the second number: '))

perform_calculations(a, b)
Out:
File "<ipython-input-13-2898daa486d2>", line 4
    a = int(input('Enter the first number: '))
    ^
IndentationError: expected an indented block

Once again, we're getting an error because of an empty suite (perform_calculations doesn't have any code). By adding in pass, we can avoid the error:

def perform_calculations(a, b):
    # add functionality later
    pass

a = int(input('Enter the first number: '))
b = int(input('Enter the second number: '))

perform_calculations(a, b)
Out:
Enter the first number: 8
Enter the second number: 4

Seeing as our script is running successfully, we can now worry about filling in our function:

def perform_calculations(a, b):
    print(f"{a} plus {b} is {a + b}")
    print(f"{a} minus {b} is {a - b}")
    print(f"{a} multiplied by {b} is {a * b}")
    print(f"{a} divided by {b} is {int(a / b)}")

a = int(input('Enter the first number: '))
b = int(input('Enter the second number: '))

perform_calculations(a, b)
Out:
Enter the first number: 8
Enter the second number: 4
8 plus 4 is 12
8 minus 4 is 4
8 multiplied by 4 is 32
8 divided by 4 is 2

Even though we omitted pass from our final script, it was handy for testing.

Class architecture placeholders

Similarly, pass can be great for designing our class architecture when writing a custom class.

By using pass as a placeholder, we can create a class' methods without writing the code for them. For an example of this, let's look at what a custom class for creating the player character in a simple video game might look like:

class Player:
    def __init__(self):
        self.health = 100
        self.mana = 100
        self.level = 1

    def take_damage(self):
        self.health -= 10

At the moment, the class has only got one instance method, take_damage, which subtracts ten from the value of the player's health attribute.

We may not have time to write them right now, but we could add a few other methods to our GameProtagonist class related to abilities the player can use:

class Player:
    def __init__(self):
        self.health = 100
        self.mana = 100
        self.level = 1

    def take_damage(self):
        self.health -= 10

    def attack(self):
        pass

    def heal(self):
        pass

    def fireball(self):
        pass

We've now added in three more methods, attack, heal and fireball. By using pass here, we've essentially created a to-do list of things to add to our GameProtagonist class at a later point in time.

When working on large scripts, you may find that your pass placeholders are kept in place for a long time before you're able to come back and fill in any empty suites, making pass statements extremely useful.

Summary

pass is a great way of exercising more controls over our scripts, hence why it's called a control statement. Whenever pass is triggered, Python will skip over it, not doing anything. This functionality is beneficial for testing and debugging, as we can use pass as a placeholder.

By adding pass to empty suites, we can prevent scripts from crashing, allowing us to see how our overall scripts are running without needing to finish coding every aspect of a script first.


Meet the Authors

alfie-grace-headshot-square2.jpg

Alfie graduated with a Master's degree in Mechanical Engineering from University College London. He's currently working as Data Scientist at Square Enix. Find him on LinkedIn.

Brendan Martin
Editor: Brendan
Founder of LearnDataSci

Get updates in your inbox

Join over 7,500 data science learners.