break — Expert Examples
Exits the nearest enclosing for or while loop immediately
Break bytecode
How break compiles to bytecode instructions.
python
import dis def with_break(): for i in range(10): if i == 5: break def with_else(): for i in range(10): if i == 5: break else: print("completed") print("Loop with break:") dis.dis(with_break) print("\nLoop with break + else:") dis.dis(with_else)
break compiles to JUMP_ABSOLUTE past the loop. When else is present, break jumps over the else block, while normal loop completion falls through to it.
Want to try these examples interactively?
Open Expert Playground