On Learn Python 12/14 I set age_is_12 to 0 which is false and then set name_is_maria as 1 which is true. The first exercise accepted 0 as false but the second exercise did not accept 1 as true, I had to type out “true”. Why is that?
Please link to the exercise. Thanks.
1 isn’t True, but True has the value 1 (bool inherits int, not the other way around)
One exercise might have tested truthyness (behaviour) and the other identity
This illustrates the special relationship that
0 have with booleans.
print int(True) # 1 print int(False) # 0 print bool(1) # True print bool(0) # False print 1 == True # True print 0 == False # True print 2 == True # False print -1 == True # False
Notice that only 1 and 0 are directly corresponding to True and False.
When we explore truthiness behavior we see how other values may be cast to a boolean…
print 2 and True # True print 0 and True # 0
When 2 is cast to a boolean, its truthiness is evident because the expression yielded the second operand, meaning the whole thing is True.
When 0 is case to a boolean, its falsiness is evident because the expression yielded the first operand, meaning it was short-circuited by a False boolean, and the whole expression is False.
This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.