bool & min/maxPython 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.
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:
False, None0, 0.0, 0j"", [], {}, set(), ()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 valueif x: would run, and why.True and False are numbersHere'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:
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
0 → False again. print() shows False. If the answer
key says "0" it's numerically right but the object Python returns is the boolean.
and / or — and what they actually returnOne 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).
a and b → returns a if a is falsy (and never looks at
b); otherwise returns b.a or b → returns a if a is truthy (and never looks at
b); otherwise returns b.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:
a and b return?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:.
False/None/0/0.0/""/[]/{}/()/set(); everything
else is truthy. (2) bool is an int — True==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.