dorothy dorothy - 27 days ago 14
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?

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


thanks

Answer

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:
Comments