Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Python

Keyword break

Exit the innermost enclosing loop and skip that loop’s else suite.

Version: Python 3.14; repository image, production runtime not attested

Syntax

for item in items:
    if stop(item):
        break

Exit the innermost enclosing loop and skip that loop’s else suite.

A break in a nested loop leaves its outer loop running. If the transfer exits a try with finally, the cleanup suite runs before execution continues after the loop.

The sample records both inner exits and the outer else.

Examples

trace = []
for row in ((2, 3), (4, 5)):
    for value in row:
        try:
            trace.append(value)
            break
        finally:
            trace.append("clean")
    else:
        trace.append("inner else")
else:
    trace.append("outer else")
print(trace)

Result

[2, 'clean', 4, 'clean', 'outer else']

The sample records both inner exits and the outer else.

Sources