We can convert to and from Boolean values in the same way we can convert between, for example, floats and strings:
float("3.14")
For the types of values we know: here is the rule for converting to Booleans: anything that's not "" or 0 converts to True, and the empty string "" and 0 convert to False:
bool(0)
bool(-0.5)
bool("Engsci is the best")
bool("False") #Not the empty string!
We can also convert bools to integers:
int(True)
int(False)
This suggests a neat trick for the "ice cream or pie" issue:
pie = True
ice_cream = False
if int(pie)+int(ice_cream) == 1:
print("I didn't lie")
We can draw a (somewhat) loose analogy: if 0 is False and 1 (or anything positive) is True, then and roughly corresponds to * and or roughly corresponds to +
1 * 1 == 1 True and True == True
1 * 0 == 0 True and False == False
1 + 0 == 1 True or False == True
In this case, not A would corresond to (1-A)
Here's an example of a common bug:
a = "abc"
if a:
print("hi")
This doesn't produce an error, since a (not being "") gets converted to True. In this case, the programmer might have meant something like if a == "aaa".