Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions bot/resources/tags/for-else.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
**for-else**

In Python it's possible to attach an `else` clause to a for loop. The code under the `else` block will be run when the iterable is exhausted (there are no more items to iterate over). Code within the else block will **not** run if the loop is broken out using `break`.

Here's an example of its usage:
```py
numbers = [1, 3, 5, 7, 9, 11]

for number in numbers:
if number % 2 == 0:
print(f"Found an even number: {number}")
break
Comment thread
vcokltfre marked this conversation as resolved.
print(f"{number} is odd.")
else:
print("All numbers are odd. How odd.")
```
Try running this example but with an even number in the list, see how the output changes as you do so.