Chapter 29 of 54

pass Statement in Python

When you plan a program, you often know its structure before you know every detail. You might know that you need an if block for adults, or a loop over students, but you haven't written the code inside yet.

In many languages, you could just leave an empty pair of braces { }. Python doesn't use braces. It uses indentation, and an indented block can't be empty. That's why Python has a tiny keyword whose only job is to do nothing: pass.

What is the pass Statement?

pass is a null statement. When Python runs it, nothing happens. It is used as a placeholder where Python's syntax needs a statement but you don't want any code to run.

if True:
    pass

This is a complete, valid program. It runs without any output and without any error.


Why Do We Need pass?

Every block that ends with a colon (if, elif, else, for, while, each case of a match, and later def and class) must contain at least one indented statement. Try leaving one empty:

age = 20

if age >= 18:

print("Done")

Output:

IndentationError: expected an indented block after 'if' statement on line 3

Python expected the body of the if and found nothing. Now add pass:

age = 20

if age >= 18:
    pass  # TODO: handle adults later

print("Done")

Output:

Done

The if condition is checked, the block runs, pass does nothing, and the program continues normally.

A Comment Is Not Enough

You might think a comment can fill the empty block. It can't:

age = 20

if age >= 18:
    # TODO: handle adults

print("Done")

Output:

IndentationError: expected an indented block after 'if' statement on line 3

Python ignores comments completely when it reads your code, so to Python this block is still empty. pass is a real statement, so it counts.


Where Can You Use pass?

1. In if, elif and else

marks = 72

if marks >= 90:
    print("Excellent")
elif marks >= 40:
    pass  # passed: nothing special to print yet
else:
    print("Please try again")

print("Result checked")

Output:

Result checked

marks is 72, so the elif branch runs and does nothing.

2. In Loops

A loop with pass as its body still runs every iteration. It just does nothing in each one:

for i in range(5):
    pass

print("Loop finished, i is", i)

Output:

Loop finished, i is 4

The loop really did go through 0, 1, 2, 3, 4; that's why i ends up as 4. pass does not stop or skip anything.

3. In match-case

You can use pass for a case you want to accept but ignore, or for a feature you haven't built yet:

choice = "2"

match choice:
    case "1":
        print("Showing marks")
    case "2":
        pass  # attendance feature coming soon
    case _:
        print("Invalid choice")

print("Menu closed")

Output:

Menu closed

4. In Functions and Classes (Coming Up)

The most common place you'll see pass is in a function or class you plan to write later:

def calculate_tax():
    pass

class Student:
    pass

We'll learn functions in the next chapter and classes later in the course. For now, just notice that their bodies follow the same rule: they can't be empty, so pass keeps the code valid.


pass vs continue vs break

This is the most important part of the chapter, and a very common interview question. All three are short keywords you may write inside a loop, but they do very different things. Let's run the same loop with each one.

With pass

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

Output:

0
1
2
3
4

pass does nothing, so print(i) still runs for 2.

With continue

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

Output:

0
1
3
4

continue skips the rest of this iteration, so 2 is not printed.

With break

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

Output:

0
1

break ends the whole loop as soon as i becomes 2.

Side by Side

i:          0   1   2   3   4
pass:       0   1   2   3   4    (nothing changes)
continue:   0   1   -   3   4    (skip this one)
break:      0   1   X            (stop the loop)

Statement

What it does

Code after it in the same iteration

Where it can be used

pass

Nothing. Just a placeholder

Still runs

Anywhere a statement is allowed

continue

Jumps to the next iteration

Skipped

Only inside loops

break

Exits the loop completely

Skipped (and so are all later iterations)

Only inside loops

See the break & continue chapter for more on those two. The key line to remember is:

pass changes nothing about the flow of the program. continue and break do.


Using pass to Sketch a Program First

A practical way to use pass is to write the skeleton of your logic first, run it to check it has no syntax errors, and then fill in the pieces one by one:

numbers = [4, -2, 7, 0]

for n in numbers:
    if n > 0:
        print(n, "is positive")
    elif n < 0:
        pass  # TODO: decide how to handle negatives
    else:
        pass  # TODO: decide how to handle zero

Output:

4 is positive
7 is positive

The program runs today, and the TODO comments remind you what's left. Once you're done, replace each pass with real code.

Don't Use pass When You Can Flip the Condition

If one branch does nothing forever, you usually don't need it at all:

# Awkward
if n < 0:
    pass
else:
    print(n)

# Cleaner
if n >= 0:
    print(n)

Both do the same thing, but the second one says what you mean directly.


pass vs ... (Ellipsis)

You may sometimes see three dots used as a placeholder:

if True:
    ...

print(...)

Output:

Ellipsis

... is a special built-in value called Ellipsis. Writing a value on a line by itself is a valid statement, so ... also keeps a block from being empty. It's popular in type hints and in "this will be filled in later" code. For beginners, pass is the clearer choice because it is a keyword whose only meaning is "do nothing".


Common Mistakes

1. Thinking pass Skips the Iteration

It doesn't. Code after pass in the loop still runs. If you want to skip to the next iteration, use continue.

2. Expecting pass to Exit a Loop

while True:
    pass   # runs forever!

This is an infinite loop that keeps the CPU busy doing nothing. Press Ctrl + C to stop it. To leave a loop, use break or make the condition become False.

3. Using a Comment Instead of pass

A block containing only a comment is still empty and causes an IndentationError.

4. Forgetting to Replace the Placeholder

A pass left behind by mistake fails silently: the program runs, but the feature simply doesn't happen. Add a TODO comment next to every temporary pass so it's easy to search for later.

5. Hiding Errors with pass

Later, in Exception Handling, you'll see code like except: pass. That silently ignores every error, which makes bugs very hard to find. Only ignore errors you expect, and only on purpose.


Interview Tips

  • "What is pass?" A null statement. It does nothing and is used where a statement is required syntactically, such as an empty function, class, loop or if block.

  • "Difference between pass, continue and break?" pass does nothing and the code continues normally; continue skips to the next loop iteration; break exits the loop. Show the range(5) example above: 0 1 2 3 4 vs 0 1 3 4 vs 0 1.

  • "Can you use continue outside a loop?" No, that's a SyntaxError. pass can be used anywhere.

  • "Why can't you use a comment instead?" Comments are ignored by the interpreter, so the block would still be empty.

  • "Why does Python need pass when C and Java don't?" Those languages mark blocks with braces, so { } can be empty. Python marks blocks with indentation, so it needs an actual statement to show where the block is.


Summary

  • pass is a statement that does nothing.

  • Python blocks can't be empty, and comments don't count, so pass acts as a placeholder.

  • You can use it in if/elif/else, loops, match-case, functions and classes.

  • pass does not change the program's flow. continue skips to the next iteration, and break exits the loop.

  • ... also works as a placeholder, but pass is clearer.

pass means "nothing to do here yet". It keeps your code valid while you build it step by step.

In the next chapter, we'll learn how to group code into reusable functions, where you'll often start with a pass placeholder.