dorothy dorothy - 1 year ago 80
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 ?


Answer Source

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:
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download