Here's my situation:
if var:
if len(var) == 5:
do something...
else:
do the same thing...
To avoid repeating the same piece of code, I would like to combine those 2 if conditions, in one. But if var is None, I can't check its length... Any idea? I would like something like this:
if var and len(var) == 5:
do something...
Did you try that? It works:
if var and len(var) == 5:
....
The and
operator doesn't evaluate the RHS if the LHS is false. Try this:
>>> False and 1/0
False
>>> True and 1/0
ZeroDivisionError: division by zero
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With