Kind-of True

Kind-of True#

See: truthiness in Python and Python truth value testing.

There are several places where you will find Python applying a test of True that is more general than simply val == True.

One example is in if statements:

val = 'a string'  # A not-empty string is True for truth testing
if val:
     print('Truth testing of "val" returned True')
Truth testing of "val" returned True

Here the if val: clause applies Python [truth value testing] to 'a string', and returns True. This is because the truth value testing algorithm returns True from an not-empty string, and False from an empty string:

another_val = ''
if another_val:
    print('No need for a message, we will not get here')

You can see the results of truth value testing using bool() in Python. For example:

print('Bool on True', bool(True))
print('Bool on False', bool(False))
print('Bool on not-empty list', bool(['some', 'elements']))
print('Bool on empty list', bool([]))
# Bool on any number other than zero evaluates as True
print('Bool on 10', bool(10))
print('Bool on -1', bool(-1))
print('Bool on 0', bool(0))
# None tests as False
print('Bool on None', bool(None))
Bool on True True
Bool on False False
Bool on not-empty list True
Bool on empty list False
Bool on 10 True
Bool on -1 True
Bool on 0 False
Bool on None False

Examples of situations in which Python uses truth value testing are if statements; while statements and assert statements.