Skip to main content
Practice

Filling in Gaps with the pass Keyword

When programming, there are times when you need to create a placeholder for code that has not yet been written or set conditions where no action should be taken.

In Python, you can use the pass keyword for this purpose. In this lesson, we will learn about the meaning and usage of pass.


What is pass in Python?

The pass keyword in Python explicitly signifies "do nothing."

It is useful for maintaining the structure of your code while providing placeholders for sections to be filled in later.

For instance, you might be writing a function or a conditional statement and have not yet settled on the specific logic. By using pass, you can maintain the code structure and avoid syntax errors.

Example of Using the pass Keyword
if not_yet_written_function():
pass

This code does nothing, but you can fill in the specific logic later.


Using pass in Conditional Statements

The pass keyword is often used in conditional statements to do nothing.

For example, if there is a condition where nothing needs to be done, but you still want to maintain the code structure, you can use pass.

Example of Using pass in a Conditional Statement
x = 10

if x > 0:
print('Executing pass statement')
# x is greater than 0, but nothing to do yet
pass
else:
print("x is 0 or less")

In this code, if x is greater than 0, the pass statement will be executed and nothing will happen.

On the other hand, if x is 0 or less, "x is 0 or less" will be printed.

By using pass in conditional statements, you can secure space for logic you will add later.

Want to learn more?

Join CodeFriends Plus membership or enroll in a course to start your journey.