Beginner Chapter 68 · 15 min read · Module 5: Control Flow
pass Statement
- After this chapter you can explain what the pass statement does.
- You can use pass as a placeholder in empty if, loop, function and class bodies.
- You can explain why an empty block without pass causes an error.
- You can tell the difference between pass, continue and break.
- You can use pass to ignore an expected exception safely.
In many languages an empty block is written with just a pair of braces. Python does not use braces. It uses indentation, and every block must contain at least one statement. So what do you write when you know a block must exist but you are not ready to fill it in yet? The answer is the pass statement.
The pass statement does absolutely nothing. When Python runs it, nothing happens and the program moves on. Its only job is to satisfy the rule that a block cannot be empty. That small job is very useful when you sketch the structure of a program, or when you really do want an action to do nothing.
In this chapter you will see pass in if statements, loops, functions and classes. You will also compare it with continue and break, because beginners often mix up these three, and you will learn a safe way to use it with exceptions.
Why Python needs pass
A block is the indented group of lines after a colon, such as the body of an if, a for, a def or a class. Python insists that the body has at least one statement. A comment does not count, because comments are ignored. If you leave the body empty, Python raises an IndentationError that says it expected an indented block.
A good habit is to write pass together with a short comment such as TODO. The comment tells you, or another reader, what the block should do in the end. Many editors even highlight TODO comments, which makes it easy to find unfinished parts. This is very helpful when a project has many files and you want to be sure nothing is forgotten before you share it.
Python also has the ... form, called the ellipsis, which many people use for the same purpose in code that describes the shape of a function. Both are fine, but pass is the traditional choice and every Python reader knows it.
pass in an if statement
Sometimes you know a branch is needed but you have no code for it yet. Write pass so the program stays valid and you can come back later.
age = 20
if age >= 18:
pass # TODO: add adult rules later
else:
print("Minor")
print("Checked")Because age is 20, the first branch runs and pass does nothing. Then the last line prints Checked. The program is valid even though the branch is unfinished.
pass in functions and classes
When you plan a program, you can list the functions and classes first and fill them in later. Each one needs a body, and pass gives it one. Calling a function that only contains pass returns None.
def send_email():
pass
class Report:
pass
result = send_email()
print(result)
r = Report()
print(type(r).__name__)The function does nothing and gives back None. The empty class can still make objects, and type(r).__name__ shows the class name. Developers also write empty classes for custom exceptions, such as class MyError(Exception): pass.
pass in loops
Inside a loop, pass means do nothing for this item, but the rest of the loop body still runs. This is the key difference from continue.
for n in [1, 2, 3]:
if n == 2:
pass
print("pass version", n)
for n in [1, 2, 3]:
if n == 2:
continue
print("continue version", n)With pass all three numbers are printed, because nothing was skipped. With continue the number 2 is not printed. Remember: pass does nothing, continue skips, break stops.
| Statement | Meaning |
|---|---|
| pass | Do nothing; the rest of the code still runs |
| continue | Skip the rest of this round of the loop |
| break | Leave the loop entirely |
pass with exceptions
You can use pass in an except block when you expect a certain error and decide to ignore it. Do this carefully and only for a specific, harmless error. Silent failures can hide real bugs.
values = ["10", "x", "30"]
total = 0
for v in values:
try:
total += int(v)
except ValueError:
pass
print(total)The text x cannot be turned into an integer, so a ValueError happens. The except block uses pass to ignore it, and the total becomes 40.
... (an ellipsis) as a placeholder body. Most programmers still prefer pass because its meaning is clearer to beginners.When to remove pass
Treat pass as a temporary marker in most cases. When you finish a function, replace it with real code. If a block is meant to stay empty for good, a short comment next to pass explains why. This keeps your code honest and easy for other people to understand.
Python syntax
if condition:
passdef function_name():
passclass ClassName:
passtry:
risky()
except ValueError:
passReal-world applications
- Sketching the structure of a program before writing details.
- Creating empty custom exception classes.
- Ignoring a harmless, expected error.
- Leaving a branch empty while testing other branches.
- Making a minimal class to hold data later.
Common mistakes
Fix: Add pass (or real code) below the comment.
Fix: pass does nothing; the lines after it still run.
Fix: Catch a specific exception and consider logging or printing a message.
Fix: Search for pass before finishing and replace the placeholders.
Important tips
- Add a TODO comment beside each pass placeholder.
- Use pass to keep the program runnable while you build it piece by piece.
- Prefer specific exceptions when you use pass in except.
- Remember a pass only function returns None.
Key points summary
- pass is a statement that does nothing.
- Blocks cannot be empty in Python.
- pass is not the same as continue or break.
- Common in placeholders for functions, classes and branches.
- Be careful when ignoring exceptions.
Exam and interview points
- pass is a null operation.
- It is used where syntax requires a statement.
- An empty block without pass gives IndentationError.
- A function with only pass returns None.
Quick revision
- pass = do nothing.
- Needed for empty blocks.
- Not the same as continue.
- Replace placeholders when the code is ready.
Frequently asked questions
What does pass do in Python?
Nothing. It is a placeholder that lets you write a block with no real action.
Is pass the same as continue?
No. continue skips the rest of the current loop round, but pass lets the rest of the code run normally.
Can I use a comment instead of pass?
No. A comment is ignored by Python, so the block would still be empty and cause an IndentationError.
What does a function with only pass return?
It returns None, like any function without a return statement.
Is it good to ignore errors with pass?
Only for a specific, expected and harmless error. Otherwise you may hide real problems.
Practise this chapter
16 practice questions Take the chapter quiz