File tree Expand file tree Collapse file tree 3 files changed +27
-3
lines changed Expand file tree Collapse file tree 3 files changed +27
-3
lines changed Original file line number Diff line number Diff line change 1
1
---
2
2
embed :
3
- title : " Formatting code on discord "
3
+ title : " Formatting code on Discord "
4
4
---
5
5
Here's how to format Python code on Discord:
6
6
Original file line number Diff line number Diff line change
1
+ ---
2
+ embed :
3
+ title : " Comparisons to `True` and `False`"
4
+ ---
5
+ It's tempting to think that if statements always need a comparison operator like ` == ` or ` != ` , but this isn't true.
6
+ If you're just checking if a value is truthy or falsey, you don't need ` == True ` or ` == False ` .
7
+ ``` py
8
+ # instead of this...
9
+ if user_input.startswith(' y' ) == True :
10
+ my_func(user_input)
11
+
12
+ # ...write this
13
+ if user_input.startswith(' y' ):
14
+ my_func(user_input)
15
+
16
+ # for false conditions, instead of this...
17
+ if user_input.startswith(' y' ) == False :
18
+ my_func(user_input)
19
+
20
+ # ...just use `not`
21
+ if not user_input.startswith(' y' ):
22
+ my_func(user_input)
23
+ ```
24
+ This also applies to expressions that use ` is True ` or ` is False ` .
Original file line number Diff line number Diff line change 2
2
embed :
3
3
title : " Pythonic way of iterating over ordered collections"
4
4
---
5
- Iterating over ` range(len(...)) ` is a common approach to accessing each item in an ordered collection .
5
+ 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 .
6
6
``` py
7
7
for i in range (len (my_list)):
8
8
do_something(my_list[i])
9
9
```
10
- The pythonic syntax is much simpler, and is guaranteed to produce elements in the same order :
10
+ It's much simpler to iterate over the list (or other sequence) directly :
11
11
``` py
12
12
for item in my_list:
13
13
do_something(item)
You can’t perform that action at this time.
0 commit comments