I help folks sharpen their Python skills with https://PythonMorsels.com 🐍🍪
YIMBY. 95% vegan.
Using the list literal syntax is faster is considered to be more idiomatic and more readable.
Using the list literal syntax is faster is considered to be more idiomatic and more readable.
numbers = list()
This *does* technically work, but I recommend against this.
Use the "list literal" syntax instead:
numbers = []
Why?
Mostly because that's the way we do it.
But why...?
numbers = list()
This *does* technically work, but I recommend against this.
Use the "list literal" syntax instead:
numbers = []
Why?
Mostly because that's the way we do it.
But why...?
So instead of this:
if flag is True:
...
Do this:
if flag:
...
Instead of checking for True, False, and non-boolean values (to raise an exception), using truthiness allows others to provide non-boolean values as truthiness indicators.
So instead of this:
if flag is True:
...
Do this:
if flag:
...
Instead of checking for True, False, and non-boolean values (to raise an exception), using truthiness allows others to provide non-boolean values as truthiness indicators.
if flag == True:
...
We prefer to check for True and False with identity:
if flag is True:
...
This is for the same reason that we prefer to check for None with identity (PEP 8 recommends it).
But instead of True/False checks...
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
if flag == True:
...
We prefer to check for True and False with identity:
if flag is True:
...
This is for the same reason that we prefer to check for None with identity (PEP 8 recommends it).
But instead of True/False checks...
• truthiness: pym.dev/truthiness/
• checking for an empty list: pym.dev/checking-for...
For practice with this topic, solve rotate_list through the final bonus: pym.dev/exercises/5d...
• truthiness: pym.dev/truthiness/
• checking for an empty list: pym.dev/checking-for...
For practice with this topic, solve rotate_list through the final bonus: pym.dev/exercises/5d...
So be careful NOT to rely on truthiness for None checks if you need to distinguish between EMPTINESS and None.
For example this:
if name:
...
Would evaluate as false/falsey if name was None OR an empty string ("").
So be careful NOT to rely on truthiness for None checks if you need to distinguish between EMPTINESS and None.
For example this:
if name:
...
Would evaluate as false/falsey if name was None OR an empty string ("").
So instead of this:
if match is not None:
...
You would write this:
if match:
...
It's common to see this convention used with regex utilities, often with a walrus operator: pym.dev/using-walrus...
So instead of this:
if match is not None:
...
You would write this:
if match:
...
It's common to see this convention used with regex utilities, often with a walrus operator: pym.dev/using-walrus...
This pattern may seem odd, but it's very common.
Once you've seen this pattern enough, the "is this non-empty" question starts to feel more sensible than "does this list have more than 0 items in it?"
This pattern may seem odd, but it's very common.
Once you've seen this pattern enough, the "is this non-empty" question starts to feel more sensible than "does this list have more than 0 items in it?"
That also means that falsiness can also be used to check for emptiness.
So for lists, this:
if not items:
...
Is equivalent to this:
if len(items) == 0:
...
That also means that falsiness can also be used to check for emptiness.
So for lists, this:
if not items:
...
Is equivalent to this:
if len(items) == 0:
...
Meaning empty collections are falsey and non-empty ones are truthy.
Meaning empty collections are falsey and non-empty ones are truthy.
I'm at 300 potential tips now and feel squeezed dry. Although I felt the same at 270... 🙃
I'm definitely getting into the "ooh this is a thing I said to a student once but have never publicly shared" territory.
I'm at 300 potential tips now and feel squeezed dry. Although I felt the same at 270... 🙃
I'm definitely getting into the "ooh this is a thing I said to a student once but have never publicly shared" territory.
Use identity checks with None because PEP 8 says so.
So instead of:
value == None
Do this:
value is None
Use identity checks with None because PEP 8 says so.
So instead of:
value == None
Do this:
value is None
I would recommend breaking this style rule but... I don't have a very good reason to break this rule!
I would recommend breaking this style rule but... I don't have a very good reason to break this rule!
So... we should use identity checks because it was declared a good idea and made its way into PEP8, which the community largely embraces as Python's style guide.
So... we should use identity checks because it was declared a good idea and made its way into PEP8, which the community largely embraces as Python's style guide.
But in my opinion, that doesn't really matter... if an object wants to claim that it's equal to None, I don't see a good reason to stop it.
But in my opinion, that doesn't really matter... if an object wants to claim that it's equal to None, I don't see a good reason to stop it.
Because that's what everyone else does, so seeing result == None looks weird.
Seriously. That's the real reason.
Well... there are 2 (less convincing) reasons:
1. It ensures that we're looking *only* for None
2. It's faster than an equality check
Because that's what everyone else does, so seeing result == None looks weird.
Seriously. That's the real reason.
Well... there are 2 (less convincing) reasons:
1. It ensures that we're looking *only* for None
2. It's faster than an equality check