That rule is partially correct. Here is the full version:
If both operands are true, and's result is its second value. If either is false, and's result is the first false value.
>>> x = 4 and 5 >>> x 5 >>> 0 and 3 0 >>> 3 and 0 0 >>> 0 and False 0 >>> False and 0 False
>>> full = True >>> empty = False >>> (full and not empty) or (not full and empty) True >>> empty = True >>> (full and not empty) or (not full and empty) False >>> full = False >>> (full and not empty) or (not full and empty) True >>> empty = False >>> (full and not empty) or (not full and empty) False
We could also use:
(full or empty) and not (full and empty)
The first half ensures that at least one variable is True; the second half ensures that they both aren't.
>>> x = 4 >>> result = x == abs(x) >>> result True >>> x = -4 >>> result = x == abs(x) >>> result False
a)
>>> population = 10000000 >>> if population < 10000000: ... print population ... >>> population = 9999999 >>> if population < 10000000: ... print population ... 9999999
b) The question is ambiguous: does "between" mean strictly between, or are the endpoints included? In the expression below, we follow the usual Python convention of including the lower endpoint but not the upper one.
>>> population = 9999999 >>> if 10000000 <= population < 35000000: ... print population ... >>> if 10000000 <= population < 35000000: ... print population ... >>> population = 10000000 >>> if 10000000 <= population < 35000000: ... print population ... 10000000
c)
>>> land_area = 1000000 >>> if population / land_area > 100: ... print "Densely populated" ... >>> land_area = 10000 >>> if population / land_area > 100: ... print "Densely populated" ...
d)
>>> land_area = 100000 >>> if population / land_area > 100: ... print "Densely populated" ... else: ... print "Sparsely populated" ... Sparsely populated >>> land_area = 10000 >>> if population / land_area > 100: ... print "Densely populated" ... else: ... print "Sparsely populated" ... Densely populated
The problem is that 2.5 is less than 7.0, so the first if is triggered and the second one is never tested. To fix it, check the pH in the opposite order:
>>> ph = 2.5 >>> if ph < 3.0: ... print "%s is VERY acidic! Be careful." % (ph) ... elif ph < 7.0: ... print "%s is acidic." % (ph) ... 2.5 is VERY acidic! Be careful.