if already asks "is this true?". Comparing against True asks a narrower question, and it's rarely the one you meant.
Before
def has_stock(count): return count > 0 in_stock = has_stock(3) if in_stock == True: print("ship it") basket = ["tea", "scones"] if len(basket) > 0 == True: print("basket has items") else: print("basket looks empty?")
ship it basket looks empty?
The second check reads like it should work, but comparisons chain: len(basket) > 0 == True means len(basket) > 0 and 0 == True, and 0 == True is false. The extra == True made a correct test wrong.
After
def has_stock(count): return count > 0 if has_stock(3): print("ship it") basket = ["tea", "scones"] if basket: print("basket has items")
ship it basket has items
Why it works
Every value in Python has a truth value. Empty containers ([], {}, ""), 0, 0.0 and None are false; almost everything else is true. if basket: asks exactly "is there anything in it?".
== True asks something else: "does this equal the value True?". That only holds for True and 1:
for value in [True, 1, 2, "yes", ["tea"]]: print(repr(value), bool(value), value == True)
True True True 1 True True 2 True False 'yes' True False ['tea'] True False
Every one of those is true in an if, but only two pass == True.
When not to use it
When True must be told apart from other truthy values, say a setting that can be True, False or a number, write if setting is True: and make that choice visible. And a NumPy array or a pandas Series refuses a plain if because it has many values at once; use .any() or .all() to say which question you mean.