Truthiness & Type Conversion Interview Questions & Answers
6 questions Updated 2026-06-18
Python interview questions on falsy values, __bool__/__len__, explicit type conversion, truthiness of empty containers, short-circuiting and/or, and is None vs == None.
A handful of values are falsy (treated as False in a boolean context):
None, False, zero of any numeric type (0, 0.0, 0j), and
empty containers/sequences ("", [], {}, (), set(), range(0)).
Almost everything else is truthy.
bool(0), bool(""), bool([]), bool(None) # all False
bool(1), bool("x"), bool([0]), bool(" ") # all True
if not items: # idiomatic empty check
print("empty")
Why it matters: idiomatic Python uses if items: rather than if len(items) > 0:.
Rule of thumb: "empty or zero or None" is falsy; everything else is truthy.
Python calls __bool__ first; if it's not defined, it falls back to
__len__ (zero length is falsy). If neither exists, the object is always
truthy — the default for plain instances.
class Box:
def __init__(self, items): self.items = items
def __len__(self): return len(self.items) # used for truthiness
bool(Box([])) # False — __len__ is 0
bool(Box([1])) # True
class Always:
def __bool__(self): return False # __bool__ wins over __len__
bool(Always()) # False
Rule of thumb: define __bool__ (or __len__) so if obj: makes sense for your
type; otherwise every instance is truthy.
Python provides constructor functions that convert between types: int(),
float(), str(), bool(), list(), tuple(), set(), dict(). They build a
new object and raise ValueError if the input can't be converted.
int("42") # 42
int("3.9") # ValueError — not a valid int literal
int(3.9) # 3 — truncates toward zero
float("3.14") # 3.14
str(255) # "255"
list("abc") # ['a', 'b', 'c']
list({1: "a"}) # [1] — iterates keys
Rule of thumb: these are explicit conversions you call yourself — Python rarely converts types implicitly, so reach for the constructor you need.
Yes. Every empty built-in container is falsy — [], {}, (), set(),
"" — and so is None. A non-empty container is truthy regardless of what it
contains, even [0] or [False].
bool([]), bool({}), bool(set()), bool("") # all False
bool([0]), bool([False]), bool({None}) # all True — non-empty!
bool(None) # False
Watch the trap: [0] is truthy because it has one element, even though that
element is falsy. Rule of thumb: container truthiness depends on length, not on
the elements' values.
and/or short-circuit and return one of their operands, not a strict
True/False. and returns the first falsy operand (or the last if all are
truthy); or returns the first truthy operand (or the last if all are falsy).
0 and 5 # 0 — first falsy, second never evaluated
2 and 5 # 5 — both truthy, returns last
0 or "default" # "default" — first truthy
None or 0 or [] # [] — all falsy, returns the last
name = user_input or "guest" # common default idiom
Rule of thumb: x or default supplies a fallback, and short-circuiting means the
right side is skipped when the result is already decided.
None is a singleton — there is exactly one None object — so is None
checks identity, which is fast and can't be fooled. == None calls __eq__,
which a class can override to return a misleading result.
if x is None: # idiomatic, reliable
...
class Weird:
def __eq__(self, other): return True
Weird() == None # True — misleading!
Weird() is None # False — correct
Rule of thumb: always compare against None, True, and False with is/
is not — PEP 8 explicitly recommends it.
Practice tests are coming soon
Get notified when interactive mock interviews and quizzes launch.