For example, say I want to find all numbers between 1 and 1000 that are divisible by 3 and 5. Would the code:
for i in range(1,1000):
if i % 3==0 and i %5 == 0:
blah
be less efficient than say
for i in range(1,1000):
if i%3==0:
if i%5==0:
blah
Does the computer check for BOTH conditions? For example, if i=10. Would the computer in the first one compute BOTH i%3 and i%5, or would it compute i%3 and then break? In that case, it would be more efficient to put the easy to check/reject condition to the left, correct?
In python and many languages there is short circuit evaluation of the boolean expressions. This means the evaluation stops as soon as are we sure about the truth value of the boolean expression. In this regard, both your code fragments are equivalent.
You could however optimise by changing the order. For example it's better to use:
if i % 5 == 0 and i % 3 == 0
The reason is that it's rarer for a number to be a multiple of 5 and so this expression will fail earlier for most of the cases.
For example, if we check for the numbers from 1 to 150, the check i % 5 == 0
will fail for 120 numbers. So we are going to perform 120 checks for i % 5 == 0
and 30 checks for i % 5 == 0
and i % 3 == 0
. This is a total of 180 checks. Similarly, for if i % 3 == 0 and i % 5 == 0
we are going to perform 100 + 2 * 50 = 200
checks.