Python pass · Examples
Beginner

Python pass Examples

Short, runnable programs that show python pass statement in action. Read the explanation, press Run, then change the code and try your own ideas. New to the topic? Start with the Python pass Statement lesson.

1Stub-first: finished parts next to placeholders

One function is already written and the other two are still pass. The program runs from top to bottom, and the None results show exactly which pieces are left to do. (return hands a value back to the caller.)

Stub-first: finished parts next to placeholders
def read_orders():
    return ["pen", "book", "lamp"]     # finished

def pack_orders():
    pass                                # still to do

def send_invoice():
    pass                                # still to do

orders = read_orders()
print("Orders read:", len(orders))
print("Packing result:", pack_orders())
print("Invoice result:", send_invoice())
Expected outputOrders read: 3
Packing result: None
Invoice result: None

2A placeholder branch that documents a case

The pause case has no behaviour yet, but writing the branch shows that you thought about it. Without pass the empty branch would be an error.

A placeholder branch that documents a case
command = "pause"
if command == "start":
    print("Starting")
elif command == "pause":
    pass        # nothing to do yet, but this case exists
else:
    print("Unknown command")
print("Handled:", command)
Expected outputHandled: pause

3An empty class used as a bag of named values

A class with only pass is still a real class. You can create an object from it and attach values to that object. (Later lessons show cleaner ways to design classes.)

An empty class used as a bag of named values
class Settings:
    pass

settings = Settings()
settings.theme = "dark"
settings.font_size = 14
print(settings.theme, settings.font_size)
Expected outputdark 14

4Ignoring one specific error on purpose

Removing a name that isn't in the list raises a ValueError. Here that is fine: either way Max ends up not on the list. The except names the exact error and the comment explains why ignoring it is safe.

Ignoring one specific error on purpose
guests = ["Ana", "Raj", "Lee"]
for name in ["Raj", "Max"]:
    try:
        guests.remove(name)
    except ValueError:
        pass            # fine: Max was never on the list
print(guests)
Expected output['Ana', 'Lee']

5Three ways to write an empty function

A docstring (a text on the first line of a function) counts as a body too, and doubles as documentation. ... and pass also work. All three functions return None.

Three ways to write an empty function
def plan_trip():
    """Will pick dates and book flights."""

def book_hotel():
    ...

def pack_bag():
    pass

print(plan_trip(), book_hotel(), pack_bag())
Expected outputNone None None

Practise next

Python pass Statement exercises → Back to the lesson

Python if, elif and else examplesPython Functions examplesPython Exceptions: try, except, else and finally examples