Python pass Statement
Table of Contents
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 laterThis is still an error. Because the comment is not code.
But with pass, it works:
if True: passNow 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(): passThis 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: passHere, 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
0124When 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: passThis 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.
passdoes nothing. It just fills space.continueskips the rest of the current iteration and goes to the next.breakstops the loop entirely.
Let me show with an example.
for i in range(5): if i == 2: pass print(i)Output
01234pass did nothing. So all numbers printed.
for i in range(5): if i == 2: continue print(i)Output
0134continue skipped 2. So 2 was not printed.
for i in range(5): if i == 2: break print(i)Output
01break 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 laterelse: 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
passThis 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.