I help folks sharpen their Python skills with https://PythonMorsels.com 🐍🍪
YIMBY. 95% vegan.
Don't use "range(len(...))" 🧵
If you find yourself using "range(len(...))" in a loop, always pause to ask "is there a better way to do this?"
There usually is.
#Python #DailyPythonTip
Don't use "range(len(...))" 🧵
If you find yourself using "range(len(...))" in a loop, always pause to ask "is there a better way to do this?"
There usually is.
#Python #DailyPythonTip
I have no interest in debating the merit of bsky-v-mastodon, purely offering help.
Reskeets welcome ✨
Prefer "for" loops over "while" loops. 🧵
Instead of this:
n = 1
while n < 10:
print(n)
n += 1
Prefer this:
for n in range(1, 10):
print(n)
When you're considering a "while" loop, ask whether there's a way to use "for" loop instead.
#Python #DailyPythonTip
Prefer "for" loops over "while" loops. 🧵
Instead of this:
n = 1
while n < 10:
print(n)
n += 1
Prefer this:
for n in range(1, 10):
print(n)
When you're considering a "while" loop, ask whether there's a way to use "for" loop instead.
#Python #DailyPythonTip
Merge iterables using asterisks, like this: "[*first, *second]" 🧵
To merge two iterables, first and second, into third don't do this:
third = list(first) + list(second)
Do this:
third = [*first, *second]
Why?...
#Python #DailyPythonTip
Merge iterables using asterisks, like this: "[*first, *second]" 🧵
To merge two iterables, first and second, into third don't do this:
third = list(first) + list(second)
Do this:
third = [*first, *second]
Why?...
#Python #DailyPythonTip
Avoid using the "in" operator on lists... especially large lists! 🧵
A containment check (using the "in" operator) on a list requires looping over the list.
To repeatedly check whether something is contained in a list, consider using a set instead.
#Python #DailyPythonTip
Avoid using the "in" operator on lists... especially large lists! 🧵
A containment check (using the "in" operator) on a list requires looping over the list.
To repeatedly check whether something is contained in a list, consider using a set instead.
#Python #DailyPythonTip
To pre-fill lists with immutable values, use self-concatenation. 🧵
#Python #DailyPythonTip
To pre-fill lists with immutable values, use self-concatenation. 🧵
#Python #DailyPythonTip
www.citationneeded.news/free-and-ope...
www.citationneeded.news/free-and-ope...
Don't forget about slicing 🧵
To get all command-line arguments but the first, you COULD do this:
import sys
arguments = list(sys.argv)
arguments.pop(0)
But slicing is better for "get all but the first":
import sys
arguments = sys.argv[1:]
#Python #DailyPythonTip
Don't forget about slicing 🧵
To get all command-line arguments but the first, you COULD do this:
import sys
arguments = list(sys.argv)
arguments.pop(0)
But slicing is better for "get all but the first":
import sys
arguments = sys.argv[1:]
#Python #DailyPythonTip
Use negative indexes.
Instead of this:
last = items[len(items)-1]
Do this:
last = items[-1]
If you're moving from a language that doesn't have negative indexes, it's easy to forget about them.
That's the whole tip! It's a short one today.
#Python #DailyPythonTip
Use negative indexes.
Instead of this:
last = items[len(items)-1]
Do this:
last = items[-1]
If you're moving from a language that doesn't have negative indexes, it's easy to forget about them.
That's the whole tip! It's a short one today.
#Python #DailyPythonTip
Copy iterables with the list() constructor. 🧵
Need to make a new list out of an existing iterable, without changing anything?
Don't use a "for" loop, a comprehension, slicing, or the copy method.
Use list()!
#Python #DailyPythonTip
Copy iterables with the list() constructor. 🧵
Need to make a new list out of an existing iterable, without changing anything?
Don't use a "for" loop, a comprehension, slicing, or the copy method.
Use list()!
#Python #DailyPythonTip
Read more on our blog:
Read more on our blog:
Use "[]" to create empty lists instead of calling "list()". 🧵
(This week's tips will all be related to lists)
#Python #DailyPythonTip
Use "[]" to create empty lists instead of calling "list()". 🧵
(This week's tips will all be related to lists)
#Python #DailyPythonTip
When checking for emptiness (or non-emptiness) in Python, use truthiness. 🧵
Instead of this:
if len(items) > 0:
...
Or this:
if len(items):
...
Do this:
if items:
...
#Python #DailyPythonTip
When checking for emptiness (or non-emptiness) in Python, use truthiness. 🧵
Instead of this:
if len(items) > 0:
...
Or this:
if len(items):
...
Do this:
if items:
...
#Python #DailyPythonTip
Based on the roll that Reuven's been on, I suspect he has a similar goal!
If you've been enjoying my tips, check out his as well!
We're definitely going to overlap many common tips, but there's no harm in seeing the same tip twice!
if x == 10 and
y == 20:
print('Yes!') # ☹️
But with parentheses, Python sees it as one line:
if (x == 10 and
y == 20): # 🙂
print('Yes!')
Or in comprehensions…
[x*5
for x in range(10)
if x % 2]
Based on the roll that Reuven's been on, I suspect he has a similar goal!
If you've been enjoying my tips, check out his as well!
We're definitely going to overlap many common tips, but there's no harm in seeing the same tip twice!
When checking for None in Python, use identity instead of equality. 🧵
Use "result is None" instead of "result == None".
Using identity works because Python's None is a sentinel value, meaning there's exactly one None object in Python.
#Python #DailyPythonTip
When checking for None in Python, use identity instead of equality. 🧵
Use "result is None" instead of "result == None".
Using identity works because Python's None is a sentinel value, meaning there's exactly one None object in Python.
#Python #DailyPythonTip
Use De Morgan's Law to make more readable Boolean expressions. 🧵
De Morgan's Law states that:
- "not (A or B)" is the same as "(not A) and (not B)"
- "not (A and B)" is the same as "(not A) or (not B)"
#Python #DailyPythonTip
Use De Morgan's Law to make more readable Boolean expressions. 🧵
De Morgan's Law states that:
- "not (A or B)" is the same as "(not A) and (not B)"
- "not (A and B)" is the same as "(not A) or (not B)"
#Python #DailyPythonTip
Avoid using "or" short-circuiting for fallback reassignments. 🧵
Instead of this:
name = name or "world"
I recommend this:
if not name:
name = "world"
That second approach is more verbose, but I also find it more readable at a glance.
#Python #DailyPythonTip
Avoid using "or" short-circuiting for fallback reassignments. 🧵
Instead of this:
name = name or "world"
I recommend this:
if not name:
name = "world"
That second approach is more verbose, but I also find it more readable at a glance.
#Python #DailyPythonTip
Use short-circuiting to collapse nested "if" statements. 🧵
These nested "if" statements:
if result:
if result.success:
print("success!")
Can instead be written with a single "if":
if result and result.success:
print("success!")
#Python #DailyPythonTip
Use short-circuiting to collapse nested "if" statements. 🧵
These nested "if" statements:
if result:
if result.success:
print("success!")
Can instead be written with a single "if":
if result and result.success:
print("success!")
#Python #DailyPythonTip
So not nothing, but also a lot less than golf.