πPython Mistakes Everyone Makes ❌
Day 10: Assuming 0, "", and [] are Errors
One common Python mistake is treating empty or zero values as errors.
❌ The Mistake
At first glance, this looks fine. But it’s misleading.
✅ The Correct Way
Because 0, "", and [] are valid values, not errors.
Using if not x: only checks emptiness, not whether something actually went wrong.
✔ What if not x Really Means
-
It checks if a value is falsy
-
It does not mean an error occurred
π§ Simple Rule to Remember
Falsy values in Python:
1. 0
2.""(empty string)
3.[](empty list)
4.{}(empty dict)
5.None
6.False
Falsy ≠ Error
π Key Takeaway
Use is None when checking for missing or invalid data.
Use if not x only when you truly mean empty.
.png)

0 Comments:
Post a Comment