# or returns the first truthy value, or the last valueprint(0or"default")
print(""or"fallback")
print(Noneor [] or"last resort")
print("first"or"second")
# Default value pattern
name = ""
display_name = name or"Anonymous"print(f"Hello, {display_name}")
# Configuration defaults
config = {}
debug = config.get("debug") orFalse
host = config.get("host") or"localhost"
port = config.get("port") or8080print(f"{host}:{port} (debug={debug})")
Output
Click "Run" to execute your code
'or' short-circuits: it returns the first truthy value. This is a common pattern for default values, but beware: 0, False, and '' are all falsy.
Challenge
Try modifying the code above to explore different behaviors. Can you extend the example to handle a new use case?