if-elif-else
if obj:bool(): Python first calls for this method; if it exists, its return value
(True/False) is used;
len(): otherwise, Python calls len(obj); if the
result is 0, it returns True; otherwise, it returns False;
True;
None, 0, 0j, ""
evaluate to False.
| Scenario | Code Example |
|---|---|
| Basic Assignment | x = a if cond else (b if cond2 else c) |
| Container Comprehension | lst = [a if cond else b for x in list] |
| Lambda Functions | func = lambda x: a if cond else b |
| Short-Circuit Logic | var = name or "Default Value" |
for, for-break-else
while, do-whileTrue.
break
continue
pass
with