Python
Programming
Pass Statement
Python Tutorial
Code Syntax

How to use pass statement?

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

The Python pass statement is a deliberate no-op. It exists so you can write syntactically valid code in places where Python requires a block, even if you do not want that block to do anything yet.

Use pass as a Placeholder

Python does not allow an empty function, class, loop, or conditional body. If you are sketching code structure before filling in the implementation, pass keeps the interpreter happy.

python
1def calculate_discount(order_total):
2    pass
3
4
5class PaymentGateway:
6    pass

Both definitions are valid, and the module will import correctly. That is useful during prototyping, test-driven development, or when you want to define an interface before the implementation exists.

Use pass in Control Flow Blocks

You can also use pass inside loops and conditionals when one branch intentionally does nothing.

python
1numbers = [1, -3, 5, -1, 8]
2
3for number in numbers:
4    if number < 0:
5        pass
6    else:
7        print(number)

This prints only the positive values. The negative branch is valid code, but it has no effect.

That said, the example is educational more than practical. In real code, it is often clearer to invert the condition or use continue if your intent is to skip the rest of the loop body.

Compare pass with Similar Statements

pass is often confused with return, continue, and break, but they do different jobs.

python
1def demo(value):
2    if value == "pass":
3        pass
4    elif value == "return":
5        return "function ended"
6    elif value == "break":
7        for item in range(3):
8            if item == 1:
9                break
10        return "loop stopped early"
11    elif value == "continue":
12        result = []
13        for item in range(3):
14            if item == 1:
15                continue
16            result.append(item)
17        return result
18
19    return "function kept running"
20
21
22print(demo("pass"))
23print(demo("return"))
24print(demo("break"))
25print(demo("continue"))

The key distinction is simple:

  • 'pass does nothing and execution continues normally.'
  • 'return exits the function.'
  • 'break exits the nearest loop.'
  • 'continue skips to the next loop iteration.'

If you expect pass to skip work, you are using the wrong statement.

A Common Real-World Use: Empty Exception Stubs During Development

While building a feature, developers sometimes leave a temporary exception handler with pass so the program stays runnable while they inspect behavior elsewhere.

python
1try:
2    value = int("42")
3except ValueError:
4    pass

This is valid syntax, but it should be temporary. In production code, silently ignoring exceptions is usually a bad idea because it hides real failures.

If you truly want to ignore an exception, add a comment explaining why or log it explicitly.

Common Pitfalls

The biggest mistake is leaving pass behind after the real implementation should exist. A placeholder inside a function can make tests pass superficially while the feature does nothing. This is especially risky in methods that are expected to return data or mutate state.

Another problem is using pass in except blocks without thinking through the consequences. Swallowed exceptions make debugging much harder because the program appears to continue successfully even though an operation failed.

Developers also sometimes think pass is a way to skip a loop iteration. It is not. The rest of the loop body still runs unless there is no more code in the block. If the goal is to move on to the next iteration, use continue.

Finally, do not overuse pass in finished code. It is most valuable as a placeholder or as a very explicit "do nothing here" marker in a narrow case. If it appears everywhere, it usually signals unclear control flow.

Summary

  • 'pass is a no-op statement that satisfies Python's requirement for an indented block.'
  • It is most useful as a placeholder in unfinished functions, classes, and branches.
  • 'pass does not skip a loop iteration and does not exit a function.'
  • Be careful with pass in except blocks because it can hide errors.
  • Remove placeholder pass statements once the real implementation is ready.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.