diff options
author | 2021-07-13 09:03:02 +0100 | |
---|---|---|
committer | 2021-07-13 08:03:02 +0000 | |
commit | 133a1349ee52cc774c3c991a0753fd12f478a010 (patch) | |
tree | 23bca00cb842fd7ebdb27004a70c3fd96fcae537 | |
parent | Merge pull request #1676 from python-discord/drop-gpl-deps (diff) |
feat: add for-else tag (#1643)
* feat: add for-else tag
Co-authored-by: Joe Banks <[email protected]>
Co-authored-by: Xithrius <[email protected]>
-rw-r--r-- | bot/resources/tags/for-else.md | 17 |
1 files changed, 17 insertions, 0 deletions
diff --git a/bot/resources/tags/for-else.md b/bot/resources/tags/for-else.md new file mode 100644 index 000000000..e102e4e75 --- /dev/null +++ b/bot/resources/tags/for-else.md @@ -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 + 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. |