I help folks sharpen their Python skills with https://PythonMorsels.com 🐍🍪
YIMBY. 95% vegan.
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.
Use sensible chained comparisons in Python, like a < b < c 🧵
#Python #DailyPythonTip
Use sensible chained comparisons in Python, like a < b < c 🧵
#Python #DailyPythonTip
If you're debugging with print() calls, use a self-documenting expression in an f-string. 🧵
A self-documenting expression involves putting an = sign at the end of a replacement field:
>>> n = 256
>>> print(f"DEBUGGING {n**2=}")
DEBUGGING n**2=65536
#Python #DailyPythonTip
If you're debugging with print() calls, use a self-documenting expression in an f-string. 🧵
A self-documenting expression involves putting an = sign at the end of a replacement field:
>>> n = 256
>>> print(f"DEBUGGING {n**2=}")
DEBUGGING n**2=65536
#Python #DailyPythonTip
It turns out, this already exists! It's called FarmKind.
It turns out, this already exists! It's called FarmKind.
Learn about the common string formatting specifications. No need to memorize them, but learn which ones exist as it'll help you remember to look them up when you need them. 🧵
#Python #DailyPythonTip
Learn about the common string formatting specifications. No need to memorize them, but learn which ones exist as it'll help you remember to look them up when you need them. 🧵
#Python #DailyPythonTip
This book challenges liberals, conservatives, and various other groups to introspect the true reasons behind their thinking on immigration.
Also it's a comic book, which makes it more fun to read.
This book challenges liberals, conservatives, and various other groups to introspect the true reasons behind their thinking on immigration.
Also it's a comic book, which makes it more fun to read.
Prefer string interpolation over concatenation in all but simple cases. 🧵
#Python #DailyPythonTip
Prefer string interpolation over concatenation in all but simple cases. 🧵
#Python #DailyPythonTip
Avoid implicit string concatenation. 🧵
In #Python, if two string literals are placed next to each other they will be implicitly concatenated.
These 2 are the same:
greeting = "Hello " "Trey"
greeting = "Hello" + "Trey"
I recommend avoiding this feature.
#DailyPythonTip
Avoid implicit string concatenation. 🧵
In #Python, if two string literals are placed next to each other they will be implicitly concatenated.
These 2 are the same:
greeting = "Hello " "Trey"
greeting = "Hello" + "Trey"
I recommend avoiding this feature.
#DailyPythonTip
Go read The Upswing (and/or watch Join or Die on Netflix).
My favorite fiction was @hankgreen.bsky.social's "An Absolutely Remarkable Thing" (and the sequel!).
I also really enjoyed 10 non-fiction books on immigration, economics, sociology, and animal welfare.
Go read The Upswing (and/or watch Join or Die on Netflix).
for auld lang syne
for auld lang syne
My favorite fiction was @hankgreen.bsky.social's "An Absolutely Remarkable Thing" (and the sequel!).
I also really enjoyed 10 non-fiction books on immigration, economics, sociology, and animal welfare.
My favorite fiction was @hankgreen.bsky.social's "An Absolutely Remarkable Thing" (and the sequel!).
I also really enjoyed 10 non-fiction books on immigration, economics, sociology, and animal welfare.