I am curious if Python will continue checking conditions in an if statement if the first condition returns False. I'm wondering about this because I want to know if best practice is try to check conditions with low time-complexity before more complex checks.
Is there any difference between these two snippets?
if condition_1() and condition_2():
do_something()
and
if condition_1():
if condition_2():
do_something()
Yes, python boolean operators do short-circuit
Both code samples are semantically equivalent, but the first is more readable, as it has lower level of nesting.
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