Truthiness, bool & min/max

Python lets any object stand in for true/false in a condition. Knowing exactly which values are "falsy" — and that True/False are really just 1/0 in disguise — is the key to reading code like exam Q3.

truthinessfalsy values bool is an intshort-circuit and/or exam Q3

Every object is either truthy or falsy

In Python you don't only write if x == 0: — you can write if x: and let the object decide. When an object appears in a boolean context (an if, a while, an and/or), Python asks it: "are you truthy or falsy?" The complete list of falsy values is short and worth memorising — everything not on it is truthy:

The traps live in the gaps. -3 is truthy (only zero is falsy, not "negative"). "0" is truthy (it's a non-empty string — the characters don't matter). [0] is truthy (a non-empty list, even though its one element is falsy). Click any chip to check it yourself:

bool(x) — click a value

Pick a value above to see whether if x: would run, and why.

True and False are numbers

Here's the fact that makes exam Q3 click: in Python bool is a subclass of int. True is literally 1 and False is literally 0 for every arithmetic and comparison purpose. So True + True == 2, False < 5 is True, and sum([True, False, True]) == 2 (a handy way to count matches). This is why min and max — which compare by value — will happily mix booleans and numbers.

But there's a twist that the exam leans on: min and max return the actual winning element, not a fresh number. So if False wins a comparison, you get the object False back — even though it compared as 0. Watch it resolve step by step:

Evaluate min(max(False, -3, -4), 2, 7)

The punchline: the result is False, not 0 — even though they're equal in value. max(False, -3, -4) compares 0, -3, -4, the biggest is 0, and the element that carried that value was False, so max hands back False. Then min(False, 2, 7) compares 0, 2, 7, the smallest is 0False again. print() shows False. If the answer key says "0" it's numerically right but the object Python returns is the boolean.

Short-circuit and / or — and what they actually return

One more piece that surprises Java developers: in Python and and or don't return True/False — they return one of the operands, and they short-circuit (stop as soon as the answer is known).

That's why the idiom name = user_input or "guest" works: if user_input is an empty string (falsy), you fall through to "guest". And why x and x.method() safely skips the call when x is None. Try a few:

What does a and b return?

Why this matters

Truthiness is everywhere in idiomatic Python and ML code: if results: (got any rows?), config.get("lr") or 0.01 (default), while queue: (drain it). The cost of the convenience is exactly the traps above — 0, "", and empty containers are falsy, which is great until a legitimate zero (a valid price of 0.0, an empty-but-real list) gets silently skipped. When zero is a meaningful value, be explicit: write if x is None: instead of if not x:.

Takeaways: (1) falsy = False/None/0/0.0/""/[]/{}/()/set(); everything else is truthy. (2) bool is an intTrue==1, False==0. (3) min/max return the winning element, so booleans can come back out. (4) and/or short-circuit and return an operand, not a strict boolean.