Python pass Statement

In the last lesson, we learned about the Python continue Statement. Now let’s use a placeholder that intentionally does nothing.

🧩 Let’s Talk About Doing Nothing

Sometimes we want to write code that does nothing.

That sounds strange, right? Why would we want to do nothing?

But it happens more often than we think.

Maybe we are writing a function, but we have not decided what it should do yet. We want to come back to it later.

Maybe we are writing an if statement, and we want to handle one case, but for another case we do not want to do anything yet.

Maybe we are creating a class, but we want to fill in the details later.

In all these cases, Python needs something inside the block. We cannot just leave it empty. Python will give us an error.

That is where pass comes in.

πŸ”§ What Is pass in Python?

pass is a statement that does nothing.

It is a placeholder. It says: β€œI know I need something here, but I do not want to do anything right now.”

Python sees pass and says: β€œOkay, I will just move on.”

πŸ’‘ Why Do We Need pass in Python?

In Python, we cannot have an empty block.

For example, this is an error:

if True:

Python expects something indented after the colon. If there is nothing, we get a SyntaxError.

We could add a comment. But comments are ignored by Python. So the block is still empty.

if True:
# TODO: do something later

This is still an error. Because the comment is not code.

But with pass, it works:

if True:
pass

Now Python is happy. The block is not empty. It contains pass. Which does nothing. But that is enough.

πŸ§ͺ Using pass as a Placeholder in Python

The most common use of pass is as a placeholder.

We are writing code. We know we need a function or a class. But we are not ready to write it yet.

def my_function():
pass

This defines a function that does nothing. Later, we can come back and fill in the details.

Without pass, we would get an error. Because the function body is empty.

πŸ“Œ Using pass in if Statements

Sometimes we want to handle one case, but not another.

age = 20
if age >= 18:
print("You can vote")
else:
pass

Here, if the age is less than 18, nothing happens. The pass makes that explicit.

We could also just not have an else. But sometimes having it there makes the logic clearer. Especially if we plan to add something later.

πŸ” Using pass in Loops

We can use pass in loops too.

for i in range(5):
if i == 3:
pass
else:
print(i)

Output

0
1
2
4

When i is 3, it does nothing. For other numbers, it prints.

We could also just use continue. But pass is sometimes used to make the intent clear.

πŸ” Using pass in Classes

When we are learning about classes, we will see pass a lot.

class MyClass:
pass

This creates an empty class. Later, we can add methods and attributes.

Without pass, we would get an error.

βš–οΈ pass vs continue vs break in Python

These three are different. Let me make it clear.

  • pass does nothing. It just fills space.
  • continue skips the rest of the current iteration and goes to the next.
  • break stops the loop entirely.

Let me show with an example.

for i in range(5):
if i == 2:
pass
print(i)

Output

0
1
2
3
4

pass did nothing. So all numbers printed.

for i in range(5):
if i == 2:
continue
print(i)

Output

0
1
3
4

continue skipped 2. So 2 was not printed.

for i in range(5):
if i == 2:
break
print(i)

Output

0
1

break stopped the loop. So only 0 and 1 printed.

⚠️ Python pass: A Real-Life Example

Let’s say we are building a program that handles different commands. But we have not implemented one of them yet.

command = input("Enter command: ")
if command == "start":
print("Starting...")
elif command == "stop":
print("Stopping...")
elif command == "restart":
pass # TODO: implement restart later
else:
print("Unknown command")

If the user enters β€œrestart”, nothing happens. But the program does not crash. It just moves on.

That is a good use of pass. It lets us write incomplete code without errors.

πŸ—οΈ Common Mistakes with Python pass

❌ Mistake 1: Using pass When We Mean continue

for i in range(5):
if i == 2:
pass
print(i)

This prints all numbers including 2. If we wanted to skip 2, we use continue.

🚫 Mistake 2: Using pass Outside a Block

pass

This is valid Python. But it does nothing. It is not an error. But it is also not useful.

πŸ›‘ Mistake 3: Forgetting That pass Is a Statement, Not a Function

pass()

This is an error. pass is not a function. We do not call it with parentheses. Just write pass.

Next, we will learn about Python Nested Loops and place one loop inside another.