From 4d1ba104374a1d53014f384a5b248ec48e89c6bb Mon Sep 17 00:00:00 2001 From: Steele Farnsworth Date: Sat, 18 Nov 2023 14:57:17 -0500 Subject: [PATCH 1/4] Capitalize "Discord" in header; Change print statement to 'Code goes here on a new line' Some users mistakenly put the `py` on a different line than the backticks, or put the code on the same line as the `py`. This is intended to subtly prompt readers not to do this. --- bot/resources/tags/codeblock.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bot/resources/tags/codeblock.md b/bot/resources/tags/codeblock.md index c2b77637e5..46e6ffc46f 100644 --- a/bot/resources/tags/codeblock.md +++ b/bot/resources/tags/codeblock.md @@ -1,11 +1,11 @@ --- embed: - title: "Formatting code on discord" + title: "Formatting code on Discord" --- Here's how to format Python code on Discord: \`\`\`py -print('Hello world!') +print('Code goes here on a new line') \`\`\` **These are backticks, not quotes.** Check [this](https://superuser.com/questions/254076/how-do-i-type-the-tick-and-backtick-characters-on-windows/254077#254077) out if you can't find the backtick key. From 96038fd9c1f6af1df62dbe875847930e0472a87a Mon Sep 17 00:00:00 2001 From: Steele Farnsworth Date: Sat, 18 Nov 2023 15:00:24 -0500 Subject: [PATCH 2/4] Rewriting of non-code sections. The previous version began with "Iterating over range-len is a common approach ...", which sounds like an endorsement. Also removed "is guaranteed to produce elements in the same order", as I don't think anyone is actually confused about that. (I wrote the previous version--this is a criticism of my own writing.) --- bot/resources/tags/range-len.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bot/resources/tags/range-len.md b/bot/resources/tags/range-len.md index 4bd377d590..76fe9051e8 100644 --- a/bot/resources/tags/range-len.md +++ b/bot/resources/tags/range-len.md @@ -2,12 +2,12 @@ embed: title: "Pythonic way of iterating over ordered collections" --- -Iterating over `range(len(...))` is a common approach to accessing each item in an ordered collection. +Beginners often iterate over `range(len(...))` because they look like Java or C-style loops, but this is almost always a bad practice in Python. ```py for i in range(len(my_list)): do_something(my_list[i]) ``` -The pythonic syntax is much simpler, and is guaranteed to produce elements in the same order: +It's much simpler to iterate over the list (or other sequence) directly: ```py for item in my_list: do_something(item) From 36f1aa2c0dd10153558253a107ad400a325a4a94 Mon Sep 17 00:00:00 2001 From: Steele Farnsworth Date: Sat, 18 Nov 2023 15:01:33 -0500 Subject: [PATCH 3/4] New tag to explain why `== True` et al are wrong. --- bot/resources/tags/equals-true.md | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 bot/resources/tags/equals-true.md diff --git a/bot/resources/tags/equals-true.md b/bot/resources/tags/equals-true.md new file mode 100644 index 0000000000..d8e1a707e1 --- /dev/null +++ b/bot/resources/tags/equals-true.md @@ -0,0 +1,24 @@ +--- +embed: + title: "Comparisons to `True` and `False`" +--- +It's tempting to think that if statements always need a comparison operator like `==` or `!=`, but this isn't true. +If you're just checking if a value is truthy or falsey, you don't need `== True` or `== False`. +```py +# instead of this... +if user_input.startswith('y') == True: + my_func(user_input) + +# ...write this +if user_input.startswith('y'): + my_func(user_input) + +# for false conditions, instead of this... +if user_input.startswith('y') == False: + my_func(user_input) + +# ...just use `not` +if not user_input.startswith('y'): + my_func(user_input) +``` +This also applies to expressions that use `is True` or `is False`. From 0ff5fad1ba3137019ee9749355f0652de5a6b45e Mon Sep 17 00:00:00 2001 From: Steele Farnsworth Date: Sun, 31 Dec 2023 12:11:22 -0500 Subject: [PATCH 4/4] Revert 'Code goes here on a new line' to 'Hello world!' --- bot/resources/tags/codeblock.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bot/resources/tags/codeblock.md b/bot/resources/tags/codeblock.md index 46e6ffc46f..c7f2990fd8 100644 --- a/bot/resources/tags/codeblock.md +++ b/bot/resources/tags/codeblock.md @@ -5,7 +5,7 @@ embed: Here's how to format Python code on Discord: \`\`\`py -print('Code goes here on a new line') +print('Hello world!') \`\`\` **These are backticks, not quotes.** Check [this](https://superuser.com/questions/254076/how-do-i-type-the-tick-and-backtick-characters-on-windows/254077#254077) out if you can't find the backtick key.