Skip to content

Commit

Permalink
Merge pull request #2817 from python-discord/swfarnsworth/tag-updates
Browse files Browse the repository at this point in the history
Tag updates
  • Loading branch information
wookie184 authored Dec 31, 2023
2 parents 8f5a763 + 0ccd4fb commit c1f29b9
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 3 deletions.
2 changes: 1 addition & 1 deletion bot/resources/tags/codeblock.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
embed:
title: "Formatting code on discord"
title: "Formatting code on Discord"
---
Here's how to format Python code on Discord:

Expand Down
24 changes: 24 additions & 0 deletions bot/resources/tags/equals-true.md
Original file line number Diff line number Diff line change
@@ -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`.
4 changes: 2 additions & 2 deletions bot/resources/tags/range-len.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit c1f29b9

Please sign in to comment.