dorothy dorothy - 7 months ago 51
Python Question

Comparing True False confusion

i have some confusion over testing values that are assigned False, True

To check for True value, we can simply just

a = True
if (a):

how about False?

if (a) <--- or should it be if (a==False), or if not a ?



From the Python Style Guide:

For sequences, (strings, lists, tuples), use the fact that empty sequences are false.

Yes: if not seq:
     if seq:

No: if len(seq)
    if not len(seq)


Don't compare boolean values to True or False using ==.

Yes: if greeting:
No: if greeting == True:
Worse: if greeting is True: