What exactly does "not" mean in an an if statement? Does it mean to proceed to the next step only if the text expression is False instead of True?
asked 04 Mar '12, 16:26
Not changes True to False or False to True. If not True means If False, If not False - means If True.
answered 04 Mar '12, 16:29
This has to do with boolean variables. 'not', as you point out, is the opposite. For example
is exactly the same as
You can combine the 3 boolean operators - not, and and or - to make very complex expressions. For example:
if not(sick or busy) and not(is_raining or is_hailing or is_snowing) and ( (have money for gas) or (can get a ride)): go surfing
In this case, the expressions are evaluated like in math.. first evaluate the most inner parenthesis and work your way outwards.
@LizP In essence, you got it right.
would be the same as:
In both, 'ok' would be printed
Furthermore, Python is one of those languages considering True a variable being assigned (I love this):
Here, 'ok' would not be printed as x is a string and is considered True, but 'not' revert it to False.