A default with or (x or y)
x or y gives you x if x counts as true; otherwise it gives you y.
Used in 72 lessons of the course, from 00.03.
or does not always return True or False. It returns one of its two operands.
Python looks at the left one. If it is truthy (not None, not "", not 0, not an empty collection), it returns it and never looks at the other. If it is falsy, it returns the right one.
name = "" or "anon"
That is why x or y works as a default: you use x if it holds something useful, and y if not. It is written this way because it is short and needs no if.
Be careful: 0 and "" are falsy too. If those are valid values for you, this pattern will replace them.
In the course you will mostly see or in conditions, like prefix.name == "lc-mastery" or (prefix / "pyvenv.cfg").is_file(): the first check runs, and only if it fails does the second. The same "first truthy one wins" rule explains both uses.
Reach for it when: a value may come back empty or as `None` and you want a fallback, or when either of two conditions is enough.
Examples
Common mistake
retries = 0 retries = retries or 3 print(retries)
Why: `0` is falsy, so `or` discards it even though the user chose it on purpose.
How to write it: Check for None explicitly: retries = 3 if retries is None else retries.
