or — Advanced Examples
Logical OR operator; returns True if at least one operand is true
or in complex expressions
Combining or with other operators.
python
# any() is the multi-value version of or checks = [False, False, True, False] print(f"any: {any(checks)}") print(f"manual or: {checks[0] or checks[1] or checks[2] or checks[3]}") # or for fallback functions def get_from_cache(key): return None # cache miss def get_from_db(key): return f"db:{key}" def get_from_api(key): return f"api:{key}" result = get_from_cache("user") or get_from_db("user") or get_from_api("user") print(f"Result: {result}") # Nested or/and def access_allowed(user, resource): is_admin = user == "admin" is_owner = user == resource.get("owner") is_public = resource.get("public", False) return is_admin or is_owner or is_public print(access_allowed("admin", {"owner": "bob"})) print(access_allowed("alice", {"owner": "bob", "public": True})) print(access_allowed("alice", {"owner": "bob"}))
Expected Output
any: True manual or: True Result: db:user True True False
'or' chains create fallback patterns: try each option until one succeeds. any() generalizes this to iterables.
Want to try these examples interactively?
Open Advanced Playground