| [ Web Proxy ] |
| Viewing: https://www.pythonbyexample.dev/examples/loop-else | [Back] [Original] |
Control Flow
If the loop reaches break, the else block is skipped. This branch means the search succeeded early.
Source
names = ["Ada", "Grace", "Guido"]
for name in names:
if name == "Grace":
print("found")
break
else:
print("missing")Output
foundIf the loop finishes without break, the else block runs. This branch means the search examined every value and found nothing.
Source
for name in names:
if name == "Linus":
print("found")
break
else:
print("missing")Output
missingNotes
else runs when the loop was not ended by break.for and while loops.found
missing
Execution time appears here after you run the example.
| Web Proxy Viewer | New URL | Original Page |