Best afterA comparison is a yes/no value
Truthiness: what counts as true
Python lets any value stand in a yes/no test: empty containers, 0, '' and None are 'falsy', everything else 'truthy'. But truthy is not the same as equal to True — and confusing the two skips the branch you wanted.
Set label to 'has items' when the list has something in it, otherwise 'empty'.
Comparing a list to True execution-derived · CPython
3 label = "has items"4else:variables
state after line 1 runs
| variable | value |
|---|---|
items | [3,8] |
step 1 / 3
Click a line, drag the slider, or use the ← → keys.
What you are looking at
Watch which branch runs, and what label ends up as.
- Buggy —
if items == True:compares the list[3, 8]to the boolTrue— and a list never equalsTrue, so the test isFalseand we wrongly land on'empty'. - Procedural —
if items:asks whether the list is truthy. A non-empty list is, so we correctly land on'has items'.
Every state you see came from running the program under CPython's tracer at build time — see how GlassBox stays honest.